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