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