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