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