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