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