arm: tegra: xmm: CP wakeup and system suspend
[linux-2.6.git] / arch / arm / mach-tegra / baseband-xmm-power.c
1 /*
2  * arch/arm/mach-tegra/baseband-xmm-power.c
3  *
4  * Copyright (C) 2011 NVIDIA Corporation
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/platform_device.h>
22 #include <linux/gpio.h>
23 #include <linux/interrupt.h>
24 #include <linux/workqueue.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <linux/fs.h>
28 #include <linux/uaccess.h>
29 #include <linux/wakelock.h>
30 #include <linux/spinlock.h>
31 #include <linux/usb.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/suspend.h>
34 #include <mach/usb_phy.h>
35 #include "board.h"
36 #include "devices.h"
37 #include "gpio-names.h"
38 #include "baseband-xmm-power.h"
39
40 MODULE_LICENSE("GPL");
41
42 unsigned long modem_ver = XMM_MODEM_VER_1130;
43 EXPORT_SYMBOL(modem_ver);
44
45 unsigned long modem_flash;
46 EXPORT_SYMBOL(modem_flash);
47
48 unsigned long modem_pm = 1;
49 EXPORT_SYMBOL(modem_pm);
50
51 unsigned long enum_delay_ms = 1000; /* ignored if !modem_flash */
52
53 module_param(modem_ver, ulong, 0644);
54 MODULE_PARM_DESC(modem_ver,
55         "baseband xmm power - modem software version");
56 module_param(modem_flash, ulong, 0644);
57 MODULE_PARM_DESC(modem_flash,
58         "baseband xmm power - modem flash (1 = flash, 0 = flashless)");
59 module_param(modem_pm, ulong, 0644);
60 MODULE_PARM_DESC(modem_pm,
61         "baseband xmm power - modem power management (1 = pm, 0 = no pm)");
62 module_param(enum_delay_ms, ulong, 0644);
63 MODULE_PARM_DESC(enum_delay_ms,
64         "baseband xmm power - delay in ms between modem on and enumeration");
65
66 static struct usb_device_id xmm_pm_ids[] = {
67         { USB_DEVICE(VENDOR_ID, PRODUCT_ID),
68         .driver_info = 0 },
69         {}
70 };
71
72
73 static struct gpio tegra_baseband_gpios[] = {
74         { -1, GPIOF_OUT_INIT_LOW,  "BB_RSTn" },
75         { -1, GPIOF_OUT_INIT_LOW,  "BB_ON"   },
76         { -1, GPIOF_OUT_INIT_LOW,  "IPC_BB_WAKE" },
77         { -1, GPIOF_IN,            "IPC_AP_WAKE" },
78         { -1, GPIOF_OUT_INIT_HIGH, "IPC_HSIC_ACTIVE" },
79         { -1, GPIOF_IN,            "IPC_HSIC_SUS_REQ" },
80 };
81
82 static enum {
83         IPC_AP_WAKE_UNINIT,
84         IPC_AP_WAKE_IRQ_READY,
85         IPC_AP_WAKE_INIT1,
86         IPC_AP_WAKE_INIT2,
87         IPC_AP_WAKE_L,
88         IPC_AP_WAKE_H,
89 } ipc_ap_wake_state;
90
91 enum baseband_xmm_powerstate_t baseband_xmm_powerstate;
92 static struct workqueue_struct *workqueue;
93 static struct work_struct init1_work;
94 static struct work_struct init2_work;
95 static struct work_struct L2_resume_work;
96 static struct baseband_power_platform_data *baseband_power_driver_data;
97 static bool register_hsic_device;
98 static struct wake_lock wakelock;
99 static struct usb_device *usbdev;
100 static bool CP_initiated_L2toL0;
101 static bool modem_power_on;
102 static int power_onoff;
103 static int reenable_autosuspend;
104 static struct work_struct autopm_resume_work;
105 static bool wakeup_pending;
106 static bool modem_sleep_flag;
107 static spinlock_t xmm_lock;
108 static DEFINE_MUTEX(xmm_onoff_mutex);
109 static bool system_suspending;
110
111 static void baseband_xmm_power_L2_resume(void);
112 static int baseband_xmm_power_driver_handle_resume(
113                         struct baseband_power_platform_data *data);
114
115 static int baseband_modem_power_on(struct baseband_power_platform_data *data)
116 {
117         /* set IPC_HSIC_ACTIVE active */
118         gpio_set_value(baseband_power_driver_data->
119                 modem.xmm.ipc_hsic_active, 1);
120
121         /* wait 20 ms */
122         mdelay(20);
123
124         /* reset / power on sequence */
125         mdelay(40);
126         gpio_set_value(data->modem.xmm.bb_rst, 1);
127         mdelay(1);
128         gpio_set_value(data->modem.xmm.bb_on, 1);
129         udelay(40);
130         gpio_set_value(data->modem.xmm.bb_on, 0);
131
132         return 0;
133 }
134
135 static int baseband_xmm_power_on(struct platform_device *device)
136 {
137         struct baseband_power_platform_data *data
138                 = (struct baseband_power_platform_data *)
139                         device->dev.platform_data;
140         int ret;
141
142         pr_debug("%s {\n", __func__);
143
144         /* check for platform data */
145         if (!data) {
146                 pr_err("%s: !data\n", __func__);
147                 return -EINVAL;
148         }
149         if (baseband_xmm_powerstate != BBXMM_PS_UNINIT)
150                 return -EINVAL;
151
152         /* reset the state machine */
153         baseband_xmm_powerstate = BBXMM_PS_INIT;
154         modem_sleep_flag = false;
155
156         if (modem_ver < XMM_MODEM_VER_1130)
157                 ipc_ap_wake_state = IPC_AP_WAKE_INIT1;
158         else
159                 ipc_ap_wake_state = IPC_AP_WAKE_INIT2;
160
161         pr_debug("%s wake_st(%d) modem version %d\n", __func__,
162                                         ipc_ap_wake_state, modem_ver);
163
164         /* register usb host controller */
165         if (!modem_flash) {
166                 pr_debug("%s - %d\n", __func__, __LINE__);
167                 /* register usb host controller only once */
168                 if (register_hsic_device) {
169                         pr_debug("%s: register usb host controller\n",
170                                 __func__);
171                         modem_power_on = true;
172                         if (data->hsic_register)
173                                 data->modem.xmm.hsic_device =
174                                                 data->hsic_register();
175                         else
176                                 pr_err("%s: hsic_register is missing\n",
177                                         __func__);
178                         register_hsic_device = false;
179                 } else {
180                         /* register usb host controller */
181                         if (data->hsic_register)
182                                 data->modem.xmm.hsic_device =
183                                                         data->hsic_register();
184                         /* turn on modem */
185                         pr_debug("%s call baseband_modem_power_on\n", __func__);
186                         baseband_modem_power_on(data);
187                 }
188         }
189         ret = enable_irq_wake(gpio_to_irq(data->modem.xmm.ipc_ap_wake));
190         if (ret < 0)
191                 pr_err("%s: enable_irq_wake error\n", __func__);
192         pr_debug("%s }\n", __func__);
193
194         return 0;
195 }
196
197 static int baseband_xmm_power_off(struct platform_device *device)
198 {
199         struct baseband_power_platform_data *data;
200         int ret;
201         unsigned long flags;
202
203         pr_debug("%s {\n", __func__);
204
205         if (baseband_xmm_powerstate == BBXMM_PS_UNINIT)
206                 return -EINVAL;
207         /* check for device / platform data */
208         if (!device) {
209                 pr_err("%s: !device\n", __func__);
210                 return -EINVAL;
211         }
212         data = (struct baseband_power_platform_data *)
213                 device->dev.platform_data;
214         if (!data) {
215                 pr_err("%s: !data\n", __func__);
216                 return -EINVAL;
217         }
218
219         ipc_ap_wake_state = IPC_AP_WAKE_UNINIT;
220         ret = disable_irq_wake(gpio_to_irq(data->modem.xmm.ipc_ap_wake));
221         if (ret < 0)
222                 pr_err("%s: disable_irq_wake error\n", __func__);
223
224         /* unregister usb host controller */
225         if (data->hsic_unregister)
226                 data->hsic_unregister(data->modem.xmm.hsic_device);
227         else
228                 pr_err("%s: hsic_unregister is missing\n", __func__);
229
230
231         /* set IPC_HSIC_ACTIVE low */
232         gpio_set_value(baseband_power_driver_data->
233                 modem.xmm.ipc_hsic_active, 0);
234
235         /* wait 20 ms */
236         mdelay(20);
237
238         /* drive bb_rst low */
239         gpio_set_value(data->modem.xmm.bb_rst, 0);
240         mdelay(1);
241
242         baseband_xmm_powerstate = BBXMM_PS_UNINIT;
243         modem_sleep_flag = false;
244         CP_initiated_L2toL0 = false;
245         spin_lock_irqsave(&xmm_lock, flags);
246         wakeup_pending = false;
247         system_suspending = false;
248         spin_unlock_irqrestore(&xmm_lock, flags);
249         /* start registration process once again on xmm on */
250         register_hsic_device = true;
251         pr_debug("%s }\n", __func__);
252
253         return 0;
254 }
255
256 static ssize_t baseband_xmm_onoff(struct device *dev,
257         struct device_attribute *attr,
258         const char *buf, size_t count)
259 {
260         int pwr;
261         int size;
262         struct platform_device *device = to_platform_device(dev);
263
264         mutex_lock(&xmm_onoff_mutex);
265
266         pr_debug("%s\n", __func__);
267
268         /* check input */
269         if (buf == NULL) {
270                 pr_err("%s: buf NULL\n", __func__);
271                 mutex_unlock(&xmm_onoff_mutex);
272                 return -EINVAL;
273         }
274         pr_debug("%s: count=%d\n", __func__, count);
275
276         /* parse input */
277         size = sscanf(buf, "%d", &pwr);
278         if (size != 1) {
279                 pr_err("%s: size=%d -EINVAL\n", __func__, size);
280                 mutex_unlock(&xmm_onoff_mutex);
281                 return -EINVAL;
282         }
283
284         if (power_onoff == pwr) {
285                 pr_err("%s: Ignored, due to same CP power state(%d)\n",
286                                                 __func__, power_onoff);
287                 mutex_unlock(&xmm_onoff_mutex);
288                 return -EINVAL;
289         }
290         power_onoff = pwr;
291         pr_debug("%s power_onoff=%d\n", __func__, power_onoff);
292
293         if (power_onoff == 0)
294                 baseband_xmm_power_off(device);
295         else if (power_onoff == 1)
296                 baseband_xmm_power_on(device);
297
298         mutex_unlock(&xmm_onoff_mutex);
299
300         return count;
301 }
302
303 static DEVICE_ATTR(xmm_onoff, S_IRUSR | S_IWUSR | S_IRGRP,
304                 NULL, baseband_xmm_onoff);
305
306
307 void baseband_xmm_set_power_status(unsigned int status)
308 {
309         struct baseband_power_platform_data *data = baseband_power_driver_data;
310         int value = 0;
311         unsigned long flags;
312
313         if (baseband_xmm_powerstate == status)
314                 return;
315         pr_debug("%s\n", __func__);
316         switch (status) {
317         case BBXMM_PS_L0:
318                 if (modem_sleep_flag) {
319                         pr_info("%s Resume from L3 without calling resume"
320                                                 "function\n",  __func__);
321                         baseband_xmm_power_driver_handle_resume(data);
322                 }
323                 pr_info("L0\n");
324                 baseband_xmm_powerstate = status;
325                 if (!wake_lock_active(&wakelock))
326                         wake_lock(&wakelock);
327                 value = gpio_get_value(data->modem.xmm.ipc_hsic_active);
328                 pr_debug("before L0 ipc_hsic_active=%d\n", value);
329                 if (!value) {
330                         pr_debug("before L0 gpio set ipc_hsic_active=1 ->\n");
331                         gpio_set_value(data->modem.xmm.ipc_hsic_active, 1);
332                 }
333                 if (modem_power_on) {
334                         modem_power_on = false;
335                         baseband_modem_power_on(data);
336                 }
337                 pr_debug("gpio host active high->\n");
338                 break;
339         case BBXMM_PS_L2:
340                 pr_info("L2\n");
341                 baseband_xmm_powerstate = status;
342                 spin_lock_irqsave(&xmm_lock, flags);
343                 if (wakeup_pending) {
344                         spin_unlock_irqrestore(&xmm_lock, flags);
345                         baseband_xmm_power_L2_resume();
346                  } else {
347                         spin_unlock_irqrestore(&xmm_lock, flags);
348                         if (wake_lock_active(&wakelock))
349                                 wake_unlock(&wakelock);
350                         modem_sleep_flag = true;
351                 }
352                 break;
353         case BBXMM_PS_L3:
354                 if (baseband_xmm_powerstate == BBXMM_PS_L2TOL0) {
355                         if (!data->modem.xmm.ipc_ap_wake) {
356                                 spin_lock_irqsave(&xmm_lock, flags);
357                                 wakeup_pending = true;
358                                 spin_unlock_irqrestore(&xmm_lock, flags);
359                                 pr_info("%s: L2 race condition-CP wakeup"
360                                                 " pending\n", __func__);
361                         }
362                 }
363                 pr_info("L3\n");
364                 baseband_xmm_powerstate = status;
365                 spin_lock_irqsave(&xmm_lock, flags);
366                 system_suspending = false;
367                 spin_unlock_irqrestore(&xmm_lock, flags);
368                 if (wake_lock_active(&wakelock)) {
369                         pr_info("%s: releasing wakelock before L3\n",
370                                 __func__);
371                         wake_unlock(&wakelock);
372                 }
373                 gpio_set_value(data->modem.xmm.ipc_hsic_active, 0);
374                 pr_debug("gpio host active low->\n");
375                 break;
376         case BBXMM_PS_L2TOL0:
377                 spin_lock_irqsave(&xmm_lock, flags);
378                 system_suspending = false;
379                 wakeup_pending = false;
380                 spin_unlock_irqrestore(&xmm_lock, flags);
381                 /* do this only from L2 state */
382                 if (baseband_xmm_powerstate == BBXMM_PS_L2) {
383                         baseband_xmm_powerstate = status;
384                         pr_debug("BB XMM POWER STATE = %d\n", status);
385                         baseband_xmm_power_L2_resume();
386                 }
387                 baseband_xmm_powerstate = status;
388                 break;
389         default:
390                 baseband_xmm_powerstate = status;
391                 break;
392         }
393         pr_debug("BB XMM POWER STATE = %d\n", status);
394 }
395 EXPORT_SYMBOL_GPL(baseband_xmm_set_power_status);
396
397 irqreturn_t baseband_xmm_power_ipc_ap_wake_irq(int irq, void *dev_id)
398 {
399         int value;
400         struct baseband_power_platform_data *data = baseband_power_driver_data;
401
402         value = gpio_get_value(data->modem.xmm.ipc_ap_wake);
403         pr_debug("%s g(%d), wake_st(%d)\n", __func__, value, ipc_ap_wake_state);
404
405         if (ipc_ap_wake_state < IPC_AP_WAKE_IRQ_READY) {
406                 pr_err("%s - spurious irq\n", __func__);
407         } else if (ipc_ap_wake_state == IPC_AP_WAKE_IRQ_READY) {
408                 if (!value) {
409                         pr_debug("%s - IPC_AP_WAKE_INIT1"
410                                 " - got falling edge\n",
411                                 __func__);
412                         /* go to IPC_AP_WAKE_INIT1 state */
413                         ipc_ap_wake_state = IPC_AP_WAKE_INIT1;
414                         /* queue work */
415                         queue_work(workqueue, &init1_work);
416                 } else {
417                         pr_debug("%s - IPC_AP_WAKE_INIT1"
418                                 " - wait for falling edge\n",
419                                 __func__);
420                 }
421         } else if (ipc_ap_wake_state == IPC_AP_WAKE_INIT1) {
422                 if (!value) {
423                         pr_debug("%s - IPC_AP_WAKE_INIT2"
424                                 " - wait for rising edge\n",
425                                 __func__);
426                 } else {
427                         pr_debug("%s - IPC_AP_WAKE_INIT2"
428                                 " - got rising edge\n",
429                                 __func__);
430                         /* go to IPC_AP_WAKE_INIT2 state */
431                         ipc_ap_wake_state = IPC_AP_WAKE_INIT2;
432                         /* queue work */
433                         queue_work(workqueue, &init2_work);
434                 }
435         } else {
436                 if (!value) {
437                         pr_debug("%s - falling\n", __func__);
438                         /* First check it a CP ack or CP wake  */
439                         value = gpio_get_value
440                                 (data->modem.xmm.ipc_bb_wake);
441                         if (value) {
442                                 pr_debug("cp ack for bb_wake\n");
443                                 ipc_ap_wake_state = IPC_AP_WAKE_L;
444                                 return IRQ_HANDLED;
445                         }
446                         spin_lock(&xmm_lock);
447                         wakeup_pending = true;
448                         if (system_suspending) {
449                                 spin_unlock(&xmm_lock);
450                                 pr_info("Set wakeup_pending = 1 in system_"
451                                         " suspending!!!\n");
452                         } else {
453                                 if (baseband_xmm_powerstate ==
454                                                         BBXMM_PS_L3) {
455                                         spin_unlock(&xmm_lock);
456                                         pr_info(" CP L3 -> L0\n");
457                                 } else if (baseband_xmm_powerstate ==
458                                                         BBXMM_PS_L2) {
459                                         CP_initiated_L2toL0 = true;
460                                         spin_unlock(&xmm_lock);
461                                         baseband_xmm_set_power_status
462                                         (BBXMM_PS_L2TOL0);
463                                 } else {
464                                         CP_initiated_L2toL0 = true;
465                                         spin_unlock(&xmm_lock);
466                                 }
467                         }
468                         /* save gpio state */
469                         ipc_ap_wake_state = IPC_AP_WAKE_L;
470                 } else {
471                         pr_debug("%s - rising\n", __func__);
472                         value = gpio_get_value
473                                 (data->modem.xmm.ipc_hsic_active);
474                         if (!value) {
475                                 pr_info("host active low: ignore request\n");
476                                 ipc_ap_wake_state = IPC_AP_WAKE_H;
477                                 return IRQ_HANDLED;
478                         }
479                         value = gpio_get_value
480                                 (data->modem.xmm.ipc_bb_wake);
481                         if (value) {
482                                 /* Clear the slave wakeup request */
483                                 gpio_set_value
484                                 (data->modem.xmm.ipc_bb_wake, 0);
485                                 pr_debug("gpio slave wakeup done ->\n");
486                         }
487                         if (reenable_autosuspend && usbdev) {
488                                         reenable_autosuspend = false;
489                                 queue_work(workqueue,
490                                         &autopm_resume_work);
491                         }
492                         modem_sleep_flag = false;
493                         baseband_xmm_set_power_status(
494                                         BBXMM_PS_L0);
495                         /* save gpio state */
496                         ipc_ap_wake_state = IPC_AP_WAKE_H;
497                 }
498         }
499
500         return IRQ_HANDLED;
501 }
502 EXPORT_SYMBOL(baseband_xmm_power_ipc_ap_wake_irq);
503
504 static void baseband_xmm_power_init1_work(struct work_struct *work)
505 {
506         int value;
507
508         pr_debug("%s {\n", __func__);
509
510         /* check if IPC_HSIC_ACTIVE high */
511         value = gpio_get_value(baseband_power_driver_data->
512                 modem.xmm.ipc_hsic_active);
513         if (value != 1) {
514                 pr_err("%s - expected IPC_HSIC_ACTIVE high!\n", __func__);
515                 return;
516         }
517
518         /* wait 100 ms */
519         mdelay(100);
520
521         /* set IPC_HSIC_ACTIVE low */
522         gpio_set_value(baseband_power_driver_data->
523                 modem.xmm.ipc_hsic_active, 0);
524
525         /* wait 10 ms */
526         mdelay(10);
527
528         /* set IPC_HSIC_ACTIVE high */
529         gpio_set_value(baseband_power_driver_data->
530                 modem.xmm.ipc_hsic_active, 1);
531
532         /* wait 20 ms */
533         mdelay(20);
534
535         pr_debug("%s }\n", __func__);
536 }
537
538 static void baseband_xmm_power_init2_work(struct work_struct *work)
539 {
540         struct baseband_power_platform_data *data = baseband_power_driver_data;
541
542         pr_debug("%s\n", __func__);
543
544         /* check input */
545         if (!data)
546                 return;
547
548         /* register usb host controller only once */
549         if (register_hsic_device) {
550                 if (data->hsic_register)
551                         data->modem.xmm.hsic_device = data->hsic_register();
552                 else
553                         pr_err("%s: hsic_register is missing\n", __func__);
554                 register_hsic_device = false;
555         }
556
557 }
558
559 static void baseband_xmm_power_autopm_resume(struct work_struct *work)
560 {
561         struct usb_interface *intf;
562
563         pr_debug("%s\n", __func__);
564         if (usbdev) {
565                 usb_lock_device(usbdev);
566                 intf = usb_ifnum_to_if(usbdev, 0);
567                 usb_autopm_get_interface(intf);
568                 usb_autopm_put_interface(intf);
569                 usb_unlock_device(usbdev);
570         }
571 }
572
573
574 /* Do the work for AP/CP initiated L2->L0 */
575 static void baseband_xmm_power_L2_resume(void)
576 {
577         struct baseband_power_platform_data *data = baseband_power_driver_data;
578         int value;
579         int delay = 10000; /* maxmum delay in msec */
580         unsigned long flags;
581
582         pr_debug("%s\n", __func__);
583
584         if (!baseband_power_driver_data)
585                 return;
586
587         /* claim the wakelock here to avoid any system suspend */
588         if (!wake_lock_active(&wakelock))
589                 wake_lock(&wakelock);
590         modem_sleep_flag = false;
591         spin_lock_irqsave(&xmm_lock, flags);
592         wakeup_pending = false;
593         spin_unlock_irqrestore(&xmm_lock, flags);
594
595         if (CP_initiated_L2toL0)  {
596                 pr_info("CP L2->L0\n");
597                 CP_initiated_L2toL0 = false;
598                 queue_work(workqueue, &L2_resume_work);
599         } else {
600                 /* set the slave wakeup request */
601                 pr_info("AP L2->L0\n");
602                 value = gpio_get_value(data->modem.xmm.ipc_ap_wake);
603                 if (value) {
604                         pr_debug("waiting for host wakeup from CP...\n");
605                         /* wake bb */
606                         gpio_set_value(data->modem.xmm.ipc_bb_wake, 1);
607                         do {
608                                 mdelay(1);
609                                 value = gpio_get_value(
610                                         data->modem.xmm.ipc_ap_wake);
611                                 delay--;
612                         } while ((value) && (delay));
613                         if (delay)
614                                 pr_debug("gpio host wakeup low <-\n");
615                         else
616                                 pr_info("!!AP L2->L0 Failed\n");
617                 } else {
618                         pr_info("CP already ready\n");
619                 }
620         }
621 }
622
623 /* Do the work for CP initiated L2->L0 */
624 static void baseband_xmm_power_L2_resume_work(struct work_struct *work)
625 {
626         struct usb_interface *intf;
627
628         pr_debug("%s {\n", __func__);
629
630         if (!usbdev)
631                 return;
632         usb_lock_device(usbdev);
633         intf = usb_ifnum_to_if(usbdev, 0);
634         if (usb_autopm_get_interface(intf) == 0)
635                 usb_autopm_put_interface(intf);
636         usb_unlock_device(usbdev);
637
638         pr_debug("} %s\n", __func__);
639 }
640
641 static void baseband_xmm_power_reset_on(void)
642 {
643         /* reset / power on sequence */
644         mdelay(40);
645         gpio_set_value(baseband_power_driver_data->modem.xmm.bb_rst, 1);
646         mdelay(1);
647         gpio_set_value(baseband_power_driver_data->modem.xmm.bb_on, 1);
648         udelay(40);
649         gpio_set_value(baseband_power_driver_data->modem.xmm.bb_on, 0);
650 }
651
652 static struct baseband_xmm_power_work_t *baseband_xmm_power_work;
653
654 static void baseband_xmm_power_work_func(struct work_struct *work)
655 {
656         struct baseband_xmm_power_work_t *bbxmm_work
657                 = (struct baseband_xmm_power_work_t *) work;
658
659         pr_debug("%s\n", __func__);
660
661         switch (bbxmm_work->state) {
662         case BBXMM_WORK_UNINIT:
663                 pr_debug("BBXMM_WORK_UNINIT\n");
664                 break;
665         case BBXMM_WORK_INIT:
666                 pr_debug("BBXMM_WORK_INIT\n");
667                 /* go to next state */
668                 bbxmm_work->state = (modem_flash && !modem_pm)
669                         ? BBXMM_WORK_INIT_FLASH_STEP1
670                         : (modem_flash && modem_pm)
671                         ? BBXMM_WORK_INIT_FLASH_PM_STEP1
672                         : (!modem_flash && modem_pm)
673                         ? BBXMM_WORK_INIT_FLASHLESS_PM_STEP1
674                         : BBXMM_WORK_UNINIT;
675                 pr_debug("Go to next state %d\n", bbxmm_work->state);
676                 queue_work(workqueue, work);
677                 break;
678         case BBXMM_WORK_INIT_FLASH_STEP1:
679                 pr_debug("BBXMM_WORK_INIT_FLASH_STEP1\n");
680                 /* register usb host controller */
681                 pr_debug("%s: register usb host controller\n", __func__);
682                 if (baseband_power_driver_data->hsic_register)
683                         baseband_power_driver_data->modem.xmm.hsic_device =
684                                 baseband_power_driver_data->hsic_register();
685                 else
686                         pr_err("%s: hsic_register is missing\n", __func__);
687                 break;
688         case BBXMM_WORK_INIT_FLASH_PM_STEP1:
689                 pr_debug("BBXMM_WORK_INIT_FLASH_PM_STEP1\n");
690                 /* [modem ver >= 1130] start with IPC_HSIC_ACTIVE low */
691                 if (modem_ver >= XMM_MODEM_VER_1130) {
692                         pr_debug("%s: ver > 1130:"
693                                 " ipc_hsic_active -> 0\n", __func__);
694                         gpio_set_value(baseband_power_driver_data->
695                                 modem.xmm.ipc_hsic_active, 0);
696                 }
697                 /* reset / power on sequence */
698                 baseband_xmm_power_reset_on();
699                 /* set power status as on */
700                 power_onoff = 1;
701                 /* optional delay
702                  * 0 = flashless
703                  *   ==> causes next step to enumerate modem boot rom
704                  *       (058b / 0041)
705                  * some delay > boot rom timeout
706                  *   ==> causes next step to enumerate modem software
707                  *       (1519 / 0020)
708                  *       (requires modem to be flash version, not flashless
709                  *       version)
710                  */
711                 if (enum_delay_ms)
712                         mdelay(enum_delay_ms);
713                 /* register usb host controller */
714                 pr_debug("%s: register usb host controller\n", __func__);
715                 if (baseband_power_driver_data->hsic_register)
716                         baseband_power_driver_data->modem.xmm.hsic_device =
717                                 baseband_power_driver_data->hsic_register();
718                 else
719                         pr_err("%s: hsic_register is missing\n", __func__);
720                 /* go to next state */
721                 bbxmm_work->state = (modem_ver < XMM_MODEM_VER_1130)
722                         ? BBXMM_WORK_INIT_FLASH_PM_VER_LT_1130_STEP1
723                         : BBXMM_WORK_INIT_FLASH_PM_VER_GE_1130_STEP1;
724                 queue_work(workqueue, work);
725                 pr_debug("Go to next state %d\n", bbxmm_work->state);
726                 break;
727         case BBXMM_WORK_INIT_FLASH_PM_VER_LT_1130_STEP1:
728                 pr_debug("BBXMM_WORK_INIT_FLASH_PM_VER_LT_1130_STEP1\n");
729                 break;
730         case BBXMM_WORK_INIT_FLASH_PM_VER_GE_1130_STEP1:
731                 pr_debug("BBXMM_WORK_INIT_FLASH_PM_VER_GE_1130_STEP1\n");
732                 break;
733         case BBXMM_WORK_INIT_FLASHLESS_PM_STEP1:
734                 pr_debug("BBXMM_WORK_INIT_FLASHLESS_PM_STEP1\n");
735                 /* go to next state */
736                 bbxmm_work->state = (modem_ver < XMM_MODEM_VER_1130)
737                         ? BBXMM_WORK_INIT_FLASHLESS_PM_VER_LT_1130_WAIT_IRQ
738                         : BBXMM_WORK_INIT_FLASHLESS_PM_VER_GE_1130_STEP1;
739                 queue_work(workqueue, work);
740                 break;
741         case BBXMM_WORK_INIT_FLASHLESS_PM_VER_LT_1130_STEP1:
742                 pr_debug("BBXMM_WORK_INIT_FLASHLESS_PM_VER_LT_1130_STEP1\n");
743                 break;
744         case BBXMM_WORK_INIT_FLASHLESS_PM_VER_GE_1130_STEP1:
745                 pr_debug("BBXMM_WORK_INIT_FLASHLESS_PM_VER_GE_1130_STEP1\n");
746                 break;
747         default:
748                 break;
749         }
750
751 }
752
753 static void baseband_xmm_device_add_handler(struct usb_device *udev)
754 {
755         struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
756         const struct usb_device_id *id;
757
758         if (intf == NULL)
759                 return;
760
761         id = usb_match_id(intf, xmm_pm_ids);
762
763         if (id) {
764                 pr_debug("persist_enabled: %u\n", udev->persist_enabled);
765                 pr_info("Add device %d <%s %s>\n", udev->devnum,
766                         udev->manufacturer, udev->product);
767                 usbdev = udev;
768                 usb_enable_autosuspend(udev);
769                 pr_info("enable autosuspend\n");
770         }
771 }
772
773 static void baseband_xmm_device_remove_handler(struct usb_device *udev)
774 {
775         if (usbdev == udev) {
776                 pr_info("Remove device %d <%s %s>\n", udev->devnum,
777                         udev->manufacturer, udev->product);
778                 usbdev = 0;
779         }
780
781 }
782
783 static int usb_xmm_notify(struct notifier_block *self, unsigned long action,
784                         void *blob)
785 {
786         switch (action) {
787         case USB_DEVICE_ADD:
788                 baseband_xmm_device_add_handler(blob);
789                 break;
790         case USB_DEVICE_REMOVE:
791                 baseband_xmm_device_remove_handler(blob);
792                 break;
793         }
794
795         return NOTIFY_OK;
796 }
797
798
799 static struct notifier_block usb_xmm_nb = {
800         .notifier_call = usb_xmm_notify,
801 };
802
803 static int baseband_xmm_power_pm_notifier_event(struct notifier_block *this,
804                                         unsigned long event, void *ptr)
805 {
806         struct baseband_power_platform_data *data = baseband_power_driver_data;
807         unsigned long flags;
808
809         if (!data)
810                 return NOTIFY_DONE;
811
812         pr_debug("%s: event %ld\n", __func__, event);
813         switch (event) {
814         case PM_SUSPEND_PREPARE:
815                 pr_debug("%s : PM_SUSPEND_PREPARE\n", __func__);
816                 if (wake_lock_active(&wakelock)) {
817                         pr_info("%s: wakelock was active, aborting suspend\n",
818                                         __func__);
819                         return NOTIFY_STOP;
820                 }
821
822                 spin_lock_irqsave(&xmm_lock, flags);
823                 if (wakeup_pending) {
824                         wakeup_pending = false;
825                         spin_unlock_irqrestore(&xmm_lock, flags);
826                         pr_info("%s : XMM busy : Abort system suspend\n",
827                                  __func__);
828                         return NOTIFY_STOP;
829                 }
830                 system_suspending = true;
831                 spin_unlock_irqrestore(&xmm_lock, flags);
832                 return NOTIFY_OK;
833         case PM_POST_SUSPEND:
834                 pr_debug("%s : PM_POST_SUSPEND\n", __func__);
835                 spin_lock_irqsave(&xmm_lock, flags);
836                 system_suspending = false;
837                 if (wakeup_pending &&
838                         (baseband_xmm_powerstate == BBXMM_PS_L2)) {
839                         wakeup_pending = false;
840                         spin_unlock_irqrestore(&xmm_lock, flags);
841                         pr_info("%s : Service Pending CP wakeup\n",
842                                 __func__);
843                         CP_initiated_L2toL0 = true;
844                         baseband_xmm_set_power_status
845                                 (BBXMM_PS_L2TOL0);
846                         return NOTIFY_OK;
847                 }
848                 wakeup_pending = false;
849                 spin_unlock_irqrestore(&xmm_lock, flags);
850                 return NOTIFY_OK;
851         }
852         return NOTIFY_DONE;
853 }
854
855 static struct notifier_block baseband_xmm_power_pm_notifier = {
856         .notifier_call = baseband_xmm_power_pm_notifier_event,
857 };
858
859
860 static int baseband_xmm_power_driver_probe(struct platform_device *device)
861 {
862         struct baseband_power_platform_data *data
863                 = (struct baseband_power_platform_data *)
864                         device->dev.platform_data;
865         struct device *dev = &device->dev;
866         unsigned long flags;
867         int err;
868
869         pr_debug("%s\n", __func__);
870         pr_debug("[XMM] enum_delay_ms=%ld\n", enum_delay_ms);
871
872         /* check for platform data */
873         if (!data)
874                 return -ENODEV;
875
876         /* check if supported modem */
877         if (data->baseband_type != BASEBAND_XMM) {
878                 pr_err("unsuppported modem\n");
879                 return -ENODEV;
880         }
881
882         /* save platform data */
883         baseband_power_driver_data = data;
884
885         /* create device file */
886         err = device_create_file(dev, &dev_attr_xmm_onoff);
887         if (err < 0) {
888                 pr_err("%s - device_create_file failed\n", __func__);
889                 return -ENODEV;
890         }
891
892         /* init wake lock */
893         wake_lock_init(&wakelock, WAKE_LOCK_SUSPEND, "baseband_xmm_power");
894
895         /* init spin lock */
896         spin_lock_init(&xmm_lock);
897         /* request baseband gpio(s) */
898         tegra_baseband_gpios[0].gpio = baseband_power_driver_data
899                 ->modem.xmm.bb_rst;
900         tegra_baseband_gpios[1].gpio = baseband_power_driver_data
901                 ->modem.xmm.bb_on;
902         tegra_baseband_gpios[2].gpio = baseband_power_driver_data
903                 ->modem.xmm.ipc_bb_wake;
904         tegra_baseband_gpios[3].gpio = baseband_power_driver_data
905                 ->modem.xmm.ipc_ap_wake;
906         tegra_baseband_gpios[4].gpio = baseband_power_driver_data
907                 ->modem.xmm.ipc_hsic_active;
908         tegra_baseband_gpios[5].gpio = baseband_power_driver_data
909                 ->modem.xmm.ipc_hsic_sus_req;
910         err = gpio_request_array(tegra_baseband_gpios,
911                 ARRAY_SIZE(tegra_baseband_gpios));
912         if (err < 0) {
913                 pr_err("%s - request gpio(s) failed\n", __func__);
914                 return -ENODEV;
915         }
916
917         /* request baseband irq(s) */
918         if (modem_flash && modem_pm) {
919                 pr_debug("%s: request_irq IPC_AP_WAKE_IRQ\n", __func__);
920                 ipc_ap_wake_state = IPC_AP_WAKE_UNINIT;
921                 err = request_threaded_irq(
922                         gpio_to_irq(data->modem.xmm.ipc_ap_wake),
923                         NULL,
924                         baseband_xmm_power_ipc_ap_wake_irq,
925                         IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
926                         "IPC_AP_WAKE_IRQ",
927                         NULL);
928                 if (err < 0) {
929                         pr_err("%s - request irq IPC_AP_WAKE_IRQ failed\n",
930                                 __func__);
931                         return err;
932                 }
933                 err = enable_irq_wake(gpio_to_irq(data->modem.xmm.ipc_ap_wake));
934                 if (err < 0)
935                         pr_err("%s: enable_irq_wake error\n", __func__);
936                 ipc_ap_wake_state = IPC_AP_WAKE_IRQ_READY;
937                 if (modem_ver >= XMM_MODEM_VER_1130) {
938                         pr_debug("%s: ver > 1130: AP_WAKE_INIT1\n", __func__);
939                         /* ver 1130 or later starts in INIT1 state */
940                         ipc_ap_wake_state = IPC_AP_WAKE_INIT1;
941                 }
942         }
943
944         /* init work queue */
945         workqueue = create_singlethread_workqueue
946                 ("baseband_xmm_power_workqueue");
947         if (!workqueue) {
948                 pr_err("cannot create workqueue\n");
949                 return -1;
950         }
951         baseband_xmm_power_work = (struct baseband_xmm_power_work_t *)
952                 kmalloc(sizeof(struct baseband_xmm_power_work_t), GFP_KERNEL);
953         if (!baseband_xmm_power_work) {
954                 pr_err("cannot allocate baseband_xmm_power_work\n");
955                 return -1;
956         }
957         INIT_WORK((struct work_struct *) baseband_xmm_power_work,
958                 baseband_xmm_power_work_func);
959         baseband_xmm_power_work->state = BBXMM_WORK_INIT;
960         queue_work(workqueue,
961                 (struct work_struct *) baseband_xmm_power_work);
962
963         /* init work objects */
964         INIT_WORK(&init1_work, baseband_xmm_power_init1_work);
965         INIT_WORK(&init2_work, baseband_xmm_power_init2_work);
966         INIT_WORK(&L2_resume_work, baseband_xmm_power_L2_resume_work);
967         INIT_WORK(&autopm_resume_work, baseband_xmm_power_autopm_resume);
968
969         /* init state variables */
970         register_hsic_device = true;
971         CP_initiated_L2toL0 = false;
972         baseband_xmm_powerstate = BBXMM_PS_UNINIT;
973         spin_lock_irqsave(&xmm_lock, flags);
974         wakeup_pending = false;
975         system_suspending = false;
976         spin_unlock_irqrestore(&xmm_lock, flags);
977
978         usb_register_notify(&usb_xmm_nb);
979         register_pm_notifier(&baseband_xmm_power_pm_notifier);
980
981         pr_debug("%s }\n", __func__);
982         return 0;
983 }
984
985 static int baseband_xmm_power_driver_remove(struct platform_device *device)
986 {
987         struct baseband_power_platform_data *data
988                 = (struct baseband_power_platform_data *)
989                         device->dev.platform_data;
990         struct device *dev = &device->dev;
991
992         pr_debug("%s\n", __func__);
993
994         /* check for platform data */
995         if (!data)
996                 return 0;
997
998         unregister_pm_notifier(&baseband_xmm_power_pm_notifier);
999         usb_unregister_notify(&usb_xmm_nb);
1000
1001         /* free work structure */
1002         kfree(baseband_xmm_power_work);
1003         baseband_xmm_power_work = (struct baseband_xmm_power_work_t *) 0;
1004
1005         /* free baseband irq(s) */
1006         if (modem_flash && modem_pm) {
1007                 free_irq(gpio_to_irq(baseband_power_driver_data
1008                         ->modem.xmm.ipc_ap_wake), NULL);
1009         }
1010
1011         /* free baseband gpio(s) */
1012         gpio_free_array(tegra_baseband_gpios,
1013                 ARRAY_SIZE(tegra_baseband_gpios));
1014
1015         /* destroy wake lock */
1016         wake_lock_destroy(&wakelock);
1017
1018         /* delete device file */
1019         device_remove_file(dev, &dev_attr_xmm_onoff);
1020
1021         /* unregister usb host controller */
1022         if (data->hsic_unregister)
1023                 data->hsic_unregister(data->modem.xmm.hsic_device);
1024         else
1025                 pr_err("%s: hsic_unregister is missing\n", __func__);
1026
1027         return 0;
1028 }
1029
1030 static int baseband_xmm_power_driver_handle_resume(
1031                         struct baseband_power_platform_data *data)
1032 {
1033         int value;
1034         int delay = 1000; /* maxmum delay in msec */
1035         unsigned long flags;
1036
1037         pr_debug("%s\n", __func__);
1038         if (!data)
1039                 return 0;
1040
1041         /* check if modem is on */
1042         if (power_onoff == 0) {
1043                 pr_debug("%s - flight mode - nop\n", __func__);
1044                 return 0;
1045         }
1046
1047         modem_sleep_flag = false;
1048         spin_lock_irqsave(&xmm_lock, flags);
1049         wakeup_pending = false;
1050         spin_unlock_irqrestore(&xmm_lock, flags);
1051
1052         /* L3->L0 */
1053         baseband_xmm_set_power_status(BBXMM_PS_L3TOL0);
1054         value = gpio_get_value(data->modem.xmm.ipc_ap_wake);
1055         if (value) {
1056                 pr_info("AP L3 -> L0\n");
1057                 pr_debug("waiting for host wakeup...\n");
1058                 /* wake bb */
1059                 gpio_set_value(data->modem.xmm.ipc_bb_wake, 1);
1060                 do {
1061                         mdelay(1);
1062                         value = gpio_get_value(
1063                                 data->modem.xmm.ipc_ap_wake);
1064                         delay--;
1065                 } while ((value) && (delay));
1066                 if (delay)
1067                         pr_debug("gpio host wakeup low <-\n");
1068                 else
1069                         pr_info("!!AP L3->L0 Failed\n");
1070
1071         } else {
1072                 pr_info("CP L3 -> L0\n");
1073         }
1074         reenable_autosuspend = true;
1075
1076         return 0;
1077
1078 }
1079
1080
1081 #ifdef CONFIG_PM
1082 static int baseband_xmm_power_driver_suspend(struct device *dev)
1083 {
1084         pr_debug("%s\n", __func__);
1085         return 0;
1086 }
1087
1088 static int baseband_xmm_power_driver_resume(struct device *dev)
1089 {
1090         struct platform_device *pdev = to_platform_device(dev);
1091         struct baseband_power_platform_data *data
1092                 = (struct baseband_power_platform_data *)
1093                         pdev->dev.platform_data;
1094
1095         pr_debug("%s\n", __func__);
1096         baseband_xmm_power_driver_handle_resume(data);
1097
1098         return 0;
1099 }
1100
1101 static int baseband_xmm_power_suspend_noirq(struct device *dev)
1102 {
1103         unsigned long flags;
1104
1105         pr_debug("%s\n", __func__);
1106         spin_lock_irqsave(&xmm_lock, flags);
1107         system_suspending = false;
1108         if (wakeup_pending) {
1109                 wakeup_pending = false;
1110                 spin_unlock_irqrestore(&xmm_lock, flags);
1111                 pr_info("%s:**Abort Suspend: reason CP WAKEUP**\n", __func__);
1112                 return -EBUSY;
1113         }
1114         spin_unlock_irqrestore(&xmm_lock, flags);
1115         return 0;
1116 }
1117
1118 static int baseband_xmm_power_resume_noirq(struct device *dev)
1119 {
1120         pr_debug("%s\n", __func__);
1121         return 0;
1122 }
1123
1124 static const struct dev_pm_ops baseband_xmm_power_dev_pm_ops = {
1125         .suspend_noirq = baseband_xmm_power_suspend_noirq,
1126         .resume_noirq = baseband_xmm_power_resume_noirq,
1127         .suspend = baseband_xmm_power_driver_suspend,
1128         .resume = baseband_xmm_power_driver_resume,
1129 };
1130 #endif
1131
1132 static struct platform_driver baseband_power_driver = {
1133         .probe = baseband_xmm_power_driver_probe,
1134         .remove = baseband_xmm_power_driver_remove,
1135         .driver = {
1136                 .name = "baseband_xmm_power",
1137 #ifdef CONFIG_PM
1138                 .pm   = &baseband_xmm_power_dev_pm_ops,
1139 #endif
1140         },
1141 };
1142
1143 static int __init baseband_xmm_power_init(void)
1144 {
1145         pr_debug("%s\n", __func__);
1146         return platform_driver_register(&baseband_power_driver);
1147 }
1148
1149 static void __exit baseband_xmm_power_exit(void)
1150 {
1151         pr_debug("%s\n", __func__);
1152         platform_driver_unregister(&baseband_power_driver);
1153 }
1154
1155 module_init(baseband_xmm_power_init)
1156 module_exit(baseband_xmm_power_exit)