misc: tegra-baseband: enable DT EHCI load/unload
[linux-3.10.git] / drivers / misc / tegra-baseband / tegra_usb_modem_power.c
1 /*
2  * Copyright (c) 2011-2015, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/platform_device.h>
23 #include <linux/platform_data/tegra_usb.h>
24 #include <linux/workqueue.h>
25 #include <linux/gpio.h>
26 #include <linux/of.h>
27 #include <linux/of_device.h>
28 #include <linux/of_gpio.h>
29 #include <linux/usb.h>
30 #include <linux/err.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/suspend.h>
33 #include <linux/slab.h>
34 #include <linux/wakelock.h>
35 #include <linux/pm_qos.h>
36 #include <linux/regulator/consumer.h>
37 #include <linux/sysedp.h>
38 #include <linux/platform_data/tegra_usb_modem_power.h>
39 #include <linux/delay.h>
40 #include <linux/fs.h>
41 #include <asm/segment.h>
42 #include <asm/uaccess.h>
43 #include <linux/platform_data/modem_thermal.h>
44 #include <linux/platform_data/sysedp_modem.h>
45 #include <linux/system-wakeup.h>
46
47 #if defined(CONFIG_ARCH_TEGRA_21x_SOC)
48 #include <linux/of_platform.h>
49 #else
50 #include <linux/dma-mapping.h>
51 #include "../../../arch/arm/mach-tegra/iomap.h"
52 #endif
53
54 #define BOOST_CPU_FREQ_MIN      1200000
55 #define BOOST_CPU_FREQ_TIMEOUT  5000
56
57 #define WAKELOCK_TIMEOUT_FOR_USB_ENUM           (HZ * 10)
58 #define WAKELOCK_TIMEOUT_FOR_REMOTE_WAKE        (HZ)
59
60 #define MAX_MODEM_EDP_STATES 10
61
62 /* default autosuspend delay in ms */
63 #define DEFAULT_AUTOSUSPEND             2000
64
65 #define XHCI_HSIC_POWER "/sys/bus/platform/devices/tegra-xhci/hsic0_power"
66
67 static int hsic_power(bool on)
68 {
69         struct file *fp = NULL;
70         mm_segment_t oldfs;
71         loff_t offset = 0;
72
73         oldfs = get_fs();
74         set_fs(get_ds());
75         fp = filp_open(XHCI_HSIC_POWER, O_RDWR, S_IRWXU);
76         if (IS_ERR(fp)) {
77                 pr_err("%s: error opening %s, error:%ld\n",
78                                 __func__, XHCI_HSIC_POWER, PTR_ERR(fp));
79                 return PTR_ERR(fp);
80         }
81
82         if (on)
83                 vfs_write(fp, "1", 1, &offset);
84         else
85                 vfs_write(fp, "0", 1, &offset);
86
87         vfs_fsync(fp, 0);
88         set_fs(oldfs);
89         filp_close(fp, NULL);
90         return 0;
91 }
92
93 #ifndef CONFIG_ARCH_TEGRA_21x_SOC
94 static u64 tegra_ehci_dmamask = DMA_BIT_MASK(64);
95
96 static struct resource tegra_usb2_resources[] = {
97         [0] = {
98                 .start  = TEGRA_USB2_BASE,
99                 .end    = TEGRA_USB2_BASE + TEGRA_USB2_SIZE - 1,
100                 .flags  = IORESOURCE_MEM,
101         },
102         [1] = {
103                 .start  = INT_USB2,
104                 .end    = INT_USB2,
105                 .flags  = IORESOURCE_IRQ,
106         },
107 };
108
109 static struct platform_device tegra_ehci2_device = {
110         .name   = "tegra-ehci",
111         .id     = 1,
112         .dev    = {
113                 .dma_mask       = &tegra_ehci_dmamask,
114                 .coherent_dma_mask = DMA_BIT_MASK(64),
115         },
116         .resource = tegra_usb2_resources,
117         .num_resources = ARRAY_SIZE(tegra_usb2_resources),
118 };
119
120 static struct tegra_usb_platform_data tegra_ehci2_hsic_baseband_pdata = {
121         .port_otg = false,
122         .has_hostpc = true,
123         .unaligned_dma_buf_supported = false,
124         .phy_intf = TEGRA_USB_PHY_INTF_HSIC,
125         .op_mode = TEGRA_USB_OPMODE_HOST,
126         .u_data.host = {
127                 .hot_plug = false,
128                 .remote_wakeup_supported = true,
129                 .power_off_on_suspend = true,
130                 .skip_resume = true,
131         },
132 };
133 #endif
134
135 struct tegra_usb_modem {
136         struct tegra_usb_modem_power_platform_data *pdata;
137         struct platform_device *pdev;
138         struct regulator *regulator; /* modem power regulator */
139         unsigned int wake_cnt;  /* remote wakeup counter */
140         unsigned int wake_irq;  /* remote wakeup irq */
141         bool wake_irq_wakeable; /* LP0 wakeable */
142         unsigned int boot_irq;  /* modem boot irq */
143         bool boot_irq_wakeable; /* LP0 wakeable */
144         struct mutex lock;
145         struct wake_lock wake_lock;     /* modem wake lock */
146         unsigned int vid;       /* modem vendor id */
147         unsigned int pid;       /* modem product id */
148         struct usb_device *xusb_roothub;        /* XUSB roothub device */
149         struct usb_device *udev;        /* modem usb device */
150         struct usb_device *parent;      /* parent device */
151         struct usb_interface *intf;     /* first modem usb interface */
152         struct workqueue_struct *wq;    /* modem workqueue */
153         struct delayed_work recovery_work;      /* modem recovery work */
154         struct delayed_work enable_nvhsic_work; /* enable xhci hsic work */
155         bool nvhsic_work_queued;        /* if hsic power work is queued*/
156         struct work_struct host_load_work;      /* usb host load work */
157         struct work_struct host_unload_work;    /* usb host unload work */
158         struct pm_qos_request cpu_boost_req; /* min CPU freq request */
159         struct work_struct cpu_boost_work;      /* CPU freq boost work */
160         struct delayed_work cpu_unboost_work;   /* CPU freq unboost work */
161         const struct tegra_modem_operations *ops;       /* modem operations */
162         unsigned int capability;        /* modem capability */
163         int system_suspend;     /* system suspend flag */
164         struct notifier_block pm_notifier;      /* pm event notifier */
165         struct notifier_block usb_notifier;     /* usb event notifier */
166         int sysfs_file_created;
167         struct platform_device *hc;     /* USB host controller */
168         struct mutex hc_lock;
169         enum { EHCI_HSIC = 0, XHCI_HSIC, XHCI_UTMI } phy_type;
170         struct platform_device *modem_thermal_pdev;
171         int pre_boost_gpio;             /* control regulator output voltage */
172         int modem_state_file_created;   /* modem_state sysfs created */
173         enum { AIRPLANE = 0, RAT_3G_LTE, RAT_2G} modem_power_state;
174         struct mutex modem_state_lock;
175         struct platform_device *modem_edp_pdev;
176 };
177
178
179 /* supported modems */
180 static const struct usb_device_id modem_list[] = {
181         {USB_DEVICE(0x1983, 0x0310),    /* Icera 450 rev1 */
182          .driver_info = TEGRA_MODEM_AUTOSUSPEND | TEGRA_MODEM_CPU_BOOST,
183          },
184         {USB_DEVICE(0x1983, 0x0321),    /* Icera 450 rev2 */
185          .driver_info = TEGRA_MODEM_AUTOSUSPEND | TEGRA_MODEM_CPU_BOOST,
186          },
187         {USB_DEVICE(0x1983, 0x0327),    /* Icera 450 5AE */
188          .driver_info = TEGRA_MODEM_AUTOSUSPEND | TEGRA_MODEM_CPU_BOOST,
189          },
190         {USB_DEVICE(0x1983, 0x0427),    /* Icera 500 5AN */
191          .driver_info = TEGRA_MODEM_AUTOSUSPEND | TEGRA_MODEM_CPU_BOOST,
192          },
193         {USB_DEVICE(0x1983, 0x1005),    /* Icera 500 5AN (BSD) */
194          .driver_info = TEGRA_MODEM_CPU_BOOST,
195         /* .driver_info = TEGRA_MODEM_AUTOSUSPEND, */
196          },
197         {USB_DEVICE(0x1983, 0x1007),    /* Icera 500 Bruce */
198          .driver_info = TEGRA_USB_HOST_RELOAD|TEGRA_MODEM_AUTOSUSPEND,
199          },
200         {}
201 };
202
203 static ssize_t load_unload_usb_host(struct device *dev,
204                                     struct device_attribute *attr,
205                                     const char *buf, size_t count);
206
207 static void cpu_freq_unboost(struct work_struct *ws)
208 {
209         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
210                                                      cpu_unboost_work.work);
211
212         pm_qos_update_request(&modem->cpu_boost_req, PM_QOS_DEFAULT_VALUE);
213 }
214
215 static void cpu_freq_boost(struct work_struct *ws)
216 {
217         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
218                                                      cpu_boost_work);
219
220         cancel_delayed_work_sync(&modem->cpu_unboost_work);
221         pm_qos_update_request(&modem->cpu_boost_req, BOOST_CPU_FREQ_MIN);
222         queue_delayed_work(modem->wq, &modem->cpu_unboost_work,
223                               msecs_to_jiffies(BOOST_CPU_FREQ_TIMEOUT));
224 }
225
226 static irqreturn_t tegra_usb_modem_wake_thread(int irq, void *data)
227 {
228         struct tegra_usb_modem *modem = (struct tegra_usb_modem *)data;
229
230         mutex_lock(&modem->lock);
231         if (modem->udev && modem->udev->state != USB_STATE_NOTATTACHED) {
232                 dev_info(&modem->pdev->dev, "remote wake (%u)\n",
233                          ++(modem->wake_cnt));
234
235                 if (!modem->system_suspend) {
236                         usb_lock_device(modem->udev);
237                         if (usb_autopm_get_interface(modem->intf) == 0)
238                                 usb_autopm_put_interface_async(modem->intf);
239                         usb_unlock_device(modem->udev);
240                 }
241         }
242         mutex_unlock(&modem->lock);
243
244         return IRQ_HANDLED;
245 }
246
247 static irqreturn_t tegra_usb_modem_boot_thread(int irq, void *data)
248 {
249         struct tegra_usb_modem *modem = (struct tegra_usb_modem *)data;
250         int v = gpio_get_value(modem->pdata->boot_gpio);
251
252         dev_info(&modem->pdev->dev, "MDM_COLDBOOT %s\n", v ? "high" : "low");
253         if (modem->capability & TEGRA_USB_HOST_RELOAD)
254                 if (!work_pending(&modem->host_load_work) &&
255                     !work_pending(&modem->host_unload_work))
256                         queue_work(modem->wq, v ? &modem->host_load_work :
257                                    &modem->host_unload_work);
258
259         /* hold wait lock to complete the enumeration */
260         wake_lock_timeout(&modem->wake_lock, WAKELOCK_TIMEOUT_FOR_USB_ENUM);
261
262         /* boost CPU freq for timing requirements on single flash platforms */
263         if ((modem->capability & TEGRA_MODEM_CPU_BOOST) &&
264              !work_pending(&modem->cpu_boost_work))
265                 queue_work(modem->wq, &modem->cpu_boost_work);
266
267         /* USB disconnect maybe on going... */
268         mutex_lock(&modem->lock);
269         if (modem->udev && modem->udev->state != USB_STATE_NOTATTACHED)
270                 pr_warn("Device is not disconnected!\n");
271         mutex_unlock(&modem->lock);
272
273         return IRQ_HANDLED;
274 }
275
276 static void tegra_usb_modem_recovery(struct work_struct *ws)
277 {
278         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
279                                                      recovery_work.work);
280
281         mutex_lock(&modem->lock);
282         if (!modem->udev) {     /* assume modem crashed */
283                 if (modem->ops && modem->ops->reset)
284                         modem->ops->reset();
285         }
286         mutex_unlock(&modem->lock);
287 }
288
289 static void enable_nvhsic_power(struct work_struct *ws)
290 {
291         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
292                                         enable_nvhsic_work.work);
293         int ret;
294
295         pr_info("Enabing XHCI HSIC\n");
296         mutex_lock(&modem->hc_lock);
297         ret = hsic_power(1);
298         if (ret) {
299                 pr_err("Failed to Enable XHCI HSIC power: %d\n", ret);
300                 modem->hc = NULL;
301                 goto error;
302         }
303         modem->hc = (struct platform_device *)1;
304 error:
305         modem->nvhsic_work_queued = false;
306         mutex_unlock(&modem->hc_lock);
307 }
308
309 static void tegra_usb_host_load(struct work_struct *ws)
310 {
311         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
312                                                      host_load_work);
313         load_unload_usb_host(&modem->pdev->dev, NULL, "1", 1);
314 }
315
316 static void tegra_usb_host_unload(struct work_struct *ws)
317 {
318         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
319                                                      host_unload_work);
320         load_unload_usb_host(&modem->pdev->dev, NULL, "0", 1);
321 }
322
323 static void modem_device_add_handler(struct tegra_usb_modem *modem,
324                                struct usb_device *udev)
325 {
326         const struct usb_device_descriptor *desc = &udev->descriptor;
327         struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
328         const struct usb_device_id *id = NULL;
329
330         if (intf) {
331                 /* only look for specific modems if modem_list is provided in
332                    platform data. Otherwise, look for the modems in the default
333                    supported modem list */
334                 if (modem->pdata->modem_list)
335                         id = usb_match_id(intf, modem->pdata->modem_list);
336                 else
337                         id = usb_match_id(intf, modem_list);
338         }
339
340         if (id) {
341                 /* hold wakelock to ensure ril has enough time to restart */
342                 wake_lock_timeout(&modem->wake_lock,
343                                   WAKELOCK_TIMEOUT_FOR_USB_ENUM);
344
345                 /* enable XUSB ELPG (autosuspend) */
346                 if (modem->phy_type == XHCI_HSIC)
347                         pm_runtime_put(&modem->xusb_roothub->dev);
348
349                 pr_info("Add device %d <%s %s>\n", udev->devnum,
350                         udev->manufacturer, udev->product);
351
352                 mutex_lock(&modem->lock);
353                 modem->udev = udev;
354                 modem->parent = udev->parent;
355                 modem->intf = intf;
356                 modem->vid = desc->idVendor;
357                 modem->pid = desc->idProduct;
358                 modem->wake_cnt = 0;
359                 modem->capability = id->driver_info;
360                 mutex_unlock(&modem->lock);
361
362                 pr_info("persist_enabled: %u\n", udev->persist_enabled);
363
364 #ifdef CONFIG_PM
365                 if (modem->capability & TEGRA_MODEM_AUTOSUSPEND) {
366                         pm_runtime_set_autosuspend_delay(&udev->dev,
367                                         modem->pdata->autosuspend_delay);
368                         usb_enable_autosuspend(udev);
369                         pr_info("enable autosuspend for %s %s\n",
370                                 udev->manufacturer, udev->product);
371                 }
372
373                 /* allow the device to wake up the system */
374                 if (udev->actconfig->desc.bmAttributes &
375                     USB_CONFIG_ATT_WAKEUP)
376                         device_set_wakeup_enable(&udev->dev, true);
377 #endif
378         }
379 }
380
381 static void modem_device_remove_handler(struct tegra_usb_modem *modem,
382                                   struct usb_device *udev)
383 {
384         const struct usb_device_descriptor *desc = &udev->descriptor;
385
386         if (desc->idVendor == modem->vid && desc->idProduct == modem->pid) {
387                 pr_info("Remove device %d <%s %s>\n", udev->devnum,
388                         udev->manufacturer, udev->product);
389
390                 /* enable XUSB ELPG (autosuspend) */
391                 if (modem->phy_type == XHCI_HSIC)
392                         pm_runtime_put(&modem->xusb_roothub->dev);
393
394                 mutex_lock(&modem->lock);
395                 modem->udev = NULL;
396                 modem->intf = NULL;
397                 modem->vid = 0;
398                 mutex_unlock(&modem->lock);
399
400                 if (modem->capability & TEGRA_MODEM_RECOVERY)
401                         queue_delayed_work(modem->wq,
402                                            &modem->recovery_work, HZ * 10);
403         }
404 }
405
406
407 static void xusb_usb2_roothub_add_handler(struct tegra_usb_modem *modem,
408                 struct usb_device *udev)
409 {
410         const struct usb_device_descriptor *desc = &udev->descriptor;
411
412         if (modem->phy_type == XHCI_HSIC &&
413                 desc->idVendor == 0x1d6b && desc->idProduct == 0x2 &&
414                 !strcmp(udev->serial, "tegra-xhci")) {
415                 pr_info("Add device %d <%s %s>\n", udev->devnum,
416                         udev->manufacturer, udev->product);
417                 mutex_lock(&modem->lock);
418                 modem->xusb_roothub = udev;
419                 mutex_unlock(&modem->lock);
420                 /* HSIC isn't enabled yet, enable it in 1 sec */
421                 if (!modem->hc) {
422                         modem->nvhsic_work_queued = true;
423                         /* disable ELPG */
424                         pm_runtime_get_sync(&modem->xusb_roothub->dev);
425                         queue_delayed_work(modem->wq,
426                                         &modem->enable_nvhsic_work, HZ);
427                 }
428         }
429 }
430
431 static void xusb_usb2_roothub_remove_handler(struct tegra_usb_modem *modem,
432                 struct usb_device *udev)
433 {
434         const struct usb_device_descriptor *desc = &udev->descriptor;
435
436         if (modem->phy_type == XHCI_HSIC &&
437                 desc->idVendor == 0x1d6b && desc->idProduct == 0x2 &&
438                 !strcmp(udev->serial, "tegra-xhci")) {
439                 pr_info("Remove device %d <%s %s>\n", udev->devnum,
440                         udev->manufacturer, udev->product);
441                 mutex_lock(&modem->lock);
442                 modem->xusb_roothub = NULL;
443                 mutex_unlock(&modem->lock);
444         }
445 }
446
447 static int mdm_usb_notifier(struct notifier_block *notifier,
448                             unsigned long usb_event, void *udev)
449 {
450         struct tegra_usb_modem *modem =
451             container_of(notifier, struct tegra_usb_modem, usb_notifier);
452
453         switch (usb_event) {
454         case USB_DEVICE_ADD:
455                 modem_device_add_handler(modem, udev);
456                 xusb_usb2_roothub_add_handler(modem, udev);
457                 break;
458         case USB_DEVICE_REMOVE:
459                 modem_device_remove_handler(modem, udev);
460                 xusb_usb2_roothub_remove_handler(modem, udev);
461                 break;
462         }
463         return NOTIFY_OK;
464 }
465
466 static int mdm_pm_notifier(struct notifier_block *notifier,
467                            unsigned long pm_event, void *unused)
468 {
469         struct tegra_usb_modem *modem =
470             container_of(notifier, struct tegra_usb_modem, pm_notifier);
471
472         mutex_lock(&modem->lock);
473         if (!modem->udev) {
474                 mutex_unlock(&modem->lock);
475                 return NOTIFY_DONE;
476         }
477
478         pr_info("%s: event %ld\n", __func__, pm_event);
479         switch (pm_event) {
480         case PM_SUSPEND_PREPARE:
481                 if (wake_lock_active(&modem->wake_lock)) {
482                         pr_warn("%s: wakelock was active, aborting suspend\n",
483                                 __func__);
484                         mutex_unlock(&modem->lock);
485                         return NOTIFY_STOP;
486                 }
487
488                 modem->system_suspend = 1;
489                 mutex_unlock(&modem->lock);
490                 return NOTIFY_OK;
491         case PM_POST_SUSPEND:
492                 modem->system_suspend = 0;
493                 mutex_unlock(&modem->lock);
494                 return NOTIFY_OK;
495         }
496
497         mutex_unlock(&modem->lock);
498         return NOTIFY_DONE;
499 }
500
501 static int mdm_request_irq(struct tegra_usb_modem *modem,
502                            irq_handler_t thread_fn,
503                            unsigned int irq_gpio,
504                            unsigned long irq_flags,
505                            const char *label,
506                            unsigned int *irq,
507                            bool *is_wakeable)
508 {
509         int ret;
510
511         /* enable IRQ for GPIO */
512         *irq = gpio_to_irq(irq_gpio);
513
514         /* request threaded irq for GPIO */
515         ret = request_threaded_irq(*irq, NULL, thread_fn, irq_flags, label,
516                                    modem);
517         if (ret) {
518                 *irq = 0;
519                 return ret;
520         }
521
522         ret = enable_irq_wake(*irq);
523         *is_wakeable = (ret) ? false : true;
524
525         return 0;
526 }
527
528 /* load USB host controller */
529 static struct platform_device *tegra_usb_host_register(
530                                 const struct tegra_usb_modem *modem)
531 {
532         const struct platform_device *hc_device =
533             modem->pdata->tegra_ehci_device;
534         struct platform_device *pdev;
535         int val;
536
537 #if defined(CONFIG_ARCH_TEGRA_21x_SOC)
538         struct property *status_prop;
539
540         /* we're registering EHCI through DT here */
541
542         /* If ehci node is unavailable, we're unable to register it later.
543          * So make it available by setting status to "okay" */
544         if (!of_device_is_available(modem->pdata->ehci_node)) {
545                 pr_info("%s(): enable ehci node\n", __func__);
546                 status_prop = of_find_property(modem->pdata->ehci_node,
547                                                 "status", NULL);
548                 if (!status_prop) {
549                         pr_err("%s(): error getting status in ehci node\n",
550                                 __func__);
551                         return NULL;
552                 }
553                 strcpy((char *)status_prop->value, "okay");
554         }
555
556         /* we have to hard-code EHCI device name here for now */
557         pdev = of_platform_device_create(modem->pdata->ehci_node,
558                                         "tegra-ehci.1", NULL);
559         if (!pdev) {
560                 pr_info("%s: error registering EHCI\n", __func__);
561                 return NULL;
562         }
563
564         return pdev;
565 #endif
566
567         pdev = platform_device_alloc(hc_device->name, hc_device->id);
568         if (!pdev)
569                 return NULL;
570
571         val = platform_device_add_resources(pdev, hc_device->resource,
572                                             hc_device->num_resources);
573         if (val)
574                 goto error;
575
576         pdev->dev.dma_mask = hc_device->dev.dma_mask;
577         pdev->dev.coherent_dma_mask = hc_device->dev.coherent_dma_mask;
578
579         val = platform_device_add_data(pdev, modem->pdata->tegra_ehci_pdata,
580                                         sizeof(struct tegra_usb_platform_data));
581         if (val)
582                 goto error;
583
584         val = platform_device_add(pdev);
585         if (val)
586                 goto error;
587
588         return pdev;
589
590 error:
591         pr_err("%s: err %d\n", __func__, val);
592         platform_device_put(pdev);
593         return NULL;
594 }
595
596 /* unload USB host controller */
597 static void tegra_usb_host_unregister(const struct tegra_usb_modem *modem)
598 {
599         struct platform_device *pdev = modem->hc;
600         if (unlikely(modem->pdata->tegra_ehci_device))
601                 platform_device_unregister(pdev);
602         else
603                 of_device_unregister(pdev);
604 }
605
606 static ssize_t show_usb_host(struct device *dev,
607                              struct device_attribute *attr, char *buf)
608 {
609         struct tegra_usb_modem *modem = dev_get_drvdata(dev);
610
611         return sprintf(buf, "%d\n", (modem->hc) ? 1 : 0);
612 }
613
614 static ssize_t load_unload_usb_host(struct device *dev,
615                                     struct device_attribute *attr,
616                                     const char *buf, size_t count)
617 {
618         struct tegra_usb_modem *modem = dev_get_drvdata(dev);
619         int host, ret;
620
621         if (sscanf(buf, "%d", &host) != 1 || host < 0 || host > 1)
622                 return -EINVAL;
623
624
625         mutex_lock(&modem->hc_lock);
626         switch (modem->phy_type) {
627         case XHCI_HSIC:
628                 dev_info(&modem->pdev->dev, "using XHCI_HSIC\n");
629                 if (host) {
630                         if (modem->xusb_roothub &&
631                                         !modem->nvhsic_work_queued) {
632                                 modem->nvhsic_work_queued = true;
633                                 /* disable ELPG */
634                                 pm_runtime_get_sync(&modem->xusb_roothub->dev);
635                                 /* enable HSIC power now */
636                                 queue_delayed_work(modem->wq,
637                                         &modem->enable_nvhsic_work, 0);
638                         }
639                 } else {
640                         if (modem->xusb_roothub) {
641                                 pr_info("Disable XHCI HSIC\n");
642                                 /* disable ELPG */
643                                 pm_runtime_get_sync(&modem->xusb_roothub->dev);
644                                 ret = hsic_power(0);
645                                 modem->hc = NULL;
646                         } else
647                                 pr_warn("xusb roothub not available!\n");
648                 }
649                 break;
650         case EHCI_HSIC:
651                 dev_info(&modem->pdev->dev, "using EHCI_HSIC\n");
652                 pr_info("%s EHCI\n", host ? "Load" : "Unload");
653                 if (host && !modem->hc) {
654                         modem->hc = tegra_usb_host_register(modem);
655                 } else if (!host && modem->hc) {
656                         tegra_usb_host_unregister(modem);
657                         modem->hc = NULL;
658                 }
659                 break;
660         case XHCI_UTMI:
661                 dev_info(&modem->pdev->dev, "using XHCI_UTMI\n");
662                 modem->hc = host ? (struct platform_device *)1 : NULL;
663                 break;
664         default:
665                 pr_warn("%s: wrong phy_type:%d\n", __func__, modem->phy_type);
666         }
667         mutex_unlock(&modem->hc_lock);
668
669         return count;
670 }
671
672 static DEVICE_ATTR(load_host, S_IRUSR | S_IWUSR, show_usb_host,
673                    load_unload_usb_host);
674
675 /* Export a new sysfs for the user land (RIL) to notify modem state
676  * Airplane mode = 0
677  * 3G/LTE mode = 1
678  * 2G mode = 2
679 */
680 static ssize_t show_modem_state(struct device *dev,
681                                 struct device_attribute *attr, char *buf)
682 {
683         struct tegra_usb_modem *modem = dev_get_drvdata(dev);
684
685         return sprintf(buf, "%d\n", modem->modem_power_state);
686 }
687
688 static ssize_t set_modem_state(struct device *dev,
689                                 struct device_attribute *attr,
690                                 const char *buf, size_t count)
691 {
692         struct tegra_usb_modem *modem = dev_get_drvdata(dev);
693         int modem_state_value;
694
695         if (sscanf(buf, "%d", &modem_state_value) != 1)
696                 return -EINVAL;
697
698         mutex_lock(&modem->modem_state_lock);
699         switch (modem_state_value) {
700         case AIRPLANE:
701                 modem->modem_power_state = modem_state_value;
702
703                 /* Release BYPASS */
704                 if (regulator_allow_bypass(modem->regulator, false))
705                         dev_warn(dev,
706                         "failed to set modem regulator in non bypass mode\n");
707
708                 /* auto PFM mode*/
709                 if (regulator_set_mode(modem->regulator, REGULATOR_MODE_NORMAL))
710                         dev_warn(dev,
711                         "failed to set modem regulator in normal mode\n");
712                 break;
713         case RAT_2G:
714                 modem->modem_power_state = modem_state_value;
715
716                 /* Release BYPASS */
717                 if (regulator_allow_bypass(modem->regulator, true))
718                         dev_warn(dev,
719                         "failed to set modem regulator in bypass mode\n");
720
721                 /* forced PWM  mode*/
722                 if (regulator_set_mode(modem->regulator, REGULATOR_MODE_FAST))
723                         dev_warn(dev,
724                         "failed to set modem regulator in fast mode\n");
725                 break;
726         case RAT_3G_LTE:
727                 modem->modem_power_state = modem_state_value;
728
729                 /* Release BYPASS */
730                 if (regulator_allow_bypass(modem->regulator, true))
731                         dev_warn(dev,
732                         "failed to set modem regulator in bypass mode\n");
733
734                 /* auto PFM mode*/
735                 if (regulator_set_mode(modem->regulator, REGULATOR_MODE_NORMAL))
736                         dev_warn(dev,
737                         "failed to set modem regulator in normal mode\n");
738                 break;
739         default:
740                 dev_warn(dev, "%s: wrong modem power state:%d\n", __func__,
741                 modem_state_value);
742         }
743         mutex_unlock(&modem->modem_state_lock);
744
745         return count;
746 }
747
748 static DEVICE_ATTR(modem_state, S_IRUSR | S_IWUSR, show_modem_state,
749                         set_modem_state);
750
751 static struct modem_thermal_platform_data thermdata = {
752         .num_zones = 0,
753 };
754
755 static struct platform_device modem_thermal_device = {
756         .name = "modem-thermal",
757         .id = -1,
758         .num_resources = 0,
759         .dev = {
760                 .platform_data = NULL,
761         },
762 };
763
764 static struct modem_edp_platform_data edpdata = {
765         .mdm_power_report = -1,
766         .consumer_name = "icemdm",
767 };
768
769 static struct platform_device modem_edp_device = {
770         .name = "sysedp_modem",
771         .id = -1,
772         .num_resources = 0,
773         .dev = {
774                 .platform_data = NULL,
775         },
776 };
777
778 static int mdm_init(struct tegra_usb_modem *modem, struct platform_device *pdev)
779 {
780         struct tegra_usb_modem_power_platform_data *pdata =
781             pdev->dev.platform_data;
782         int ret = 0;
783
784         modem->pdata = pdata;
785         modem->pdev = pdev;
786
787 #ifndef CONFIG_ARCH_TEGRA_21x_SOC
788         /* WAR to load statically defined ehci device/pdata */
789         pdata->tegra_ehci_device = &tegra_ehci2_device;
790         pdata->tegra_ehci_pdata = &tegra_ehci2_hsic_baseband_pdata;
791 #endif
792
793         pdata->autosuspend_delay = DEFAULT_AUTOSUSPEND;
794
795         /* get modem operations from platform data */
796         modem->ops = (const struct tegra_modem_operations *)pdata->ops;
797
798         /* modem init */
799         if (modem->ops && modem->ops->init) {
800                 ret = modem->ops->init();
801                 if (ret)
802                         goto error;
803         }
804
805         mutex_init(&(modem->lock));
806         mutex_init(&modem->hc_lock);
807         wake_lock_init(&modem->wake_lock, WAKE_LOCK_SUSPEND, "mdm_lock");
808
809         /* if wake gpio is not specified we rely on native usb remote wake */
810         if (gpio_is_valid(pdata->wake_gpio)) {
811                 /* request remote wakeup irq from platform data */
812                 ret = mdm_request_irq(modem,
813                                       tegra_usb_modem_wake_thread,
814                                       pdata->wake_gpio,
815                                       pdata->wake_irq_flags,
816                                       "mdm_wake",
817                                       &modem->wake_irq,
818                                       &modem->wake_irq_wakeable);
819                 if (ret) {
820                         dev_err(&pdev->dev, "request wake irq error\n");
821                         goto error;
822                 }
823         }
824
825         if (gpio_is_valid(pdata->boot_gpio)) {
826                 /* request boot irq from platform data */
827                 ret = mdm_request_irq(modem,
828                                       tegra_usb_modem_boot_thread,
829                                       pdata->boot_gpio,
830                                       pdata->boot_irq_flags,
831                                       "mdm_boot",
832                                       &modem->boot_irq,
833                                       &modem->boot_irq_wakeable);
834                 if (ret) {
835                         dev_err(&pdev->dev, "request boot irq error\n");
836                         goto error;
837                 }
838         } else
839                 dev_warn(&pdev->dev, "boot irq not specified\n");
840
841         /* create sysfs node to load/unload host controller */
842         ret = device_create_file(&pdev->dev, &dev_attr_load_host);
843         if (ret) {
844                 dev_err(&pdev->dev, "can't create sysfs file\n");
845                 goto error;
846         }
847         modem->sysfs_file_created = 1;
848         modem->capability = TEGRA_USB_HOST_RELOAD;
849         if (pdev->id >= 0)
850                 dev_set_name(&pdev->dev, "MDM%d", pdev->id);
851         else
852                 dev_set_name(&pdev->dev, "MDM");
853
854         /* create sysfs node for RIL to report modem power state */
855         /* only if a regulator has been requested          */
856         modem->modem_state_file_created = 0;
857         if (pdata->regulator_name) {
858                 ret = device_create_file(&pdev->dev, &dev_attr_modem_state);
859                 if (ret) {
860                         dev_err(&pdev->dev,
861                         "can't create modem state sysfs file\n");
862                         goto error;
863                 }
864                 modem->modem_state_file_created = 1;
865                 mutex_init(&modem->modem_state_lock);
866         }
867
868         /* create work queue platform_driver_registe */
869         modem->wq = create_workqueue("tegra_usb_mdm_queue");
870         INIT_DELAYED_WORK(&modem->recovery_work, tegra_usb_modem_recovery);
871         INIT_DELAYED_WORK(&modem->enable_nvhsic_work, enable_nvhsic_power);
872         INIT_WORK(&modem->host_load_work, tegra_usb_host_load);
873         INIT_WORK(&modem->host_unload_work, tegra_usb_host_unload);
874         INIT_WORK(&modem->cpu_boost_work, cpu_freq_boost);
875         INIT_DELAYED_WORK(&modem->cpu_unboost_work, cpu_freq_unboost);
876
877         pm_qos_add_request(&modem->cpu_boost_req, PM_QOS_CPU_FREQ_MIN,
878                            PM_QOS_DEFAULT_VALUE);
879
880         modem->pm_notifier.notifier_call = mdm_pm_notifier;
881         modem->usb_notifier.notifier_call = mdm_usb_notifier;
882
883         usb_register_notify(&modem->usb_notifier);
884         register_pm_notifier(&modem->pm_notifier);
885
886         if (pdata->num_temp_sensors) {
887                 thermdata.num_zones = pdata->num_temp_sensors;
888                 modem_thermal_device.dev.platform_data = &thermdata;
889                 modem->modem_thermal_pdev = &modem_thermal_device;
890                 platform_device_register(modem->modem_thermal_pdev);
891         }
892
893         if (pdata->mdm_power_report_gpio) {
894                 edpdata.mdm_power_report = pdata->mdm_power_report_gpio;
895                 modem_edp_device.dev.platform_data = &edpdata;
896                 modem->modem_edp_pdev = &modem_edp_device;
897                 platform_device_register(modem->modem_edp_pdev);
898         }
899
900         /* start modem */
901         if (modem->ops && modem->ops->start)
902                 modem->ops->start();
903
904         return ret;
905 error:
906         if (modem->sysfs_file_created)
907                 device_remove_file(&pdev->dev, &dev_attr_load_host);
908
909         if (modem->wake_irq)
910                 free_irq(modem->wake_irq, modem);
911
912         if (modem->boot_irq)
913                 free_irq(modem->boot_irq, modem);
914
915         if (modem->modem_state_file_created)
916                 device_remove_file(&pdev->dev, &dev_attr_modem_state);
917
918         return ret;
919 }
920
921 static int tegra_usb_modem_parse_dt(struct tegra_usb_modem *modem,
922                 struct platform_device *pdev)
923 {
924         struct tegra_usb_modem_power_platform_data pdata;
925         struct device_node *node = pdev->dev.of_node;
926         struct device_node *phy_node;
927         const unsigned int *prop;
928         int gpio;
929         int ret;
930         const char *node_status;
931 #if defined(CONFIG_ARCH_TEGRA_21x_SOC)
932         struct device_node *ehci_node = NULL;
933 #endif
934
935         if (!node) {
936                 dev_err(&pdev->dev, "Missing device tree node\n");
937                 return -EINVAL;
938         }
939
940         memset(&pdata, 0, sizeof(pdata));
941
942         /* turn on modem regulator if required */
943         pdata.regulator_name = of_get_property(node, "nvidia,regulator", NULL);
944         if (pdata.regulator_name) {
945                 modem->regulator = devm_regulator_get(&pdev->dev,
946                                 pdata.regulator_name);
947                 if (IS_ERR(modem->regulator)) {
948                         dev_err(&pdev->dev, "failed to get regulator %s\n",
949                                 pdata.regulator_name);
950                         return PTR_ERR(modem->regulator);
951                 }
952                 ret = regulator_enable(modem->regulator);
953                 if (ret) {
954                         dev_err(&pdev->dev, "failed to enable regulator %s\n",
955                                 pdata.regulator_name);
956                         return ret;
957                 }
958
959                 dev_info(&pdev->dev, "set modem regulator:%s\n",
960                 pdata.regulator_name);
961
962                 /* Enable regulator bypass */
963                 if (regulator_allow_bypass(modem->regulator, true))
964                         dev_warn(&pdev->dev,
965                         "failed to set modem regulator in bypass mode\n");
966                 else
967                         dev_info(&pdev->dev,
968                         "set modem regulator in bypass mode");
969
970                 /* Enable autoPFM */
971                 if (regulator_set_mode(modem->regulator, REGULATOR_MODE_NORMAL))
972                         dev_warn(&pdev->dev,
973                         "failed to set modem regulator in normal mode\n");
974                 else
975                         dev_info(&pdev->dev,
976                         "set modem regulator in normal mode");
977
978                 /* Set modem_power_state */
979                 modem->modem_power_state = RAT_3G_LTE;
980         }
981
982         /* determine phy type */
983         modem->phy_type = -1;
984         ret = of_property_read_u32(node, "nvidia,phy-type", &modem->phy_type);
985         if (0 == ret) {
986                 dev_info(&pdev->dev,
987                         "set phy type with property 'nvidia,phy-type'\n");
988         } else {
989                 dev_info(&pdev->dev,
990                         "set phy type with child node 'nvidia,phy-*hci-*'\n");
991                 for_each_child_of_node(node, phy_node) {
992                         ret = of_property_read_string(phy_node,
993                                                         "status", &node_status);
994                         if (ret != 0) {
995                                 dev_err(&pdev->dev,
996                                         "DT property '%s/status' read fail!\n",
997                                         phy_node->full_name);
998                                 goto error;
999                         }
1000
1001                         if (strcmp(node_status, "okay") == 0) {
1002                                 if (strcmp(phy_node->name,
1003                                                 "nvidia,phy-ehci-hsic") == 0)
1004                                         modem->phy_type = EHCI_HSIC;
1005                                 else if (strcmp(phy_node->name,
1006                                                 "nvidia,phy-xhci-hsic") == 0)
1007                                         modem->phy_type = XHCI_HSIC;
1008                                 else if (strcmp(phy_node->name,
1009                                                 "nvidia,phy-xhci-utmi") == 0)
1010                                         modem->phy_type = XHCI_UTMI;
1011                                 else {
1012                                         dev_err(&pdev->dev,
1013                                                 "Unrecognized phy type node!!\n");
1014                                         ret = -EINVAL;
1015                                         goto error;
1016                                 }
1017                         }
1018                 }
1019         }
1020         if (-1 == modem->phy_type) {
1021                 dev_err(&pdev->dev,
1022                         "Unable to set phy type!!\n");
1023                 ret = -EINVAL;
1024                 goto error;
1025         }
1026
1027 #if defined(CONFIG_ARCH_TEGRA_21x_SOC)
1028         /* get EHCI device/pdata handle */
1029         if (modem->phy_type == EHCI_HSIC) {
1030                 ehci_node = of_parse_phandle(node, "nvidia,ehci-device", 0);
1031                 if (!ehci_node) {
1032                         dev_err(&pdev->dev, "can't find nvidia,ehci-device\n");
1033                         return -EINVAL;
1034                 }
1035                 /* set ehci device/pdata */
1036                 pdata.ehci_node = ehci_node;
1037         }
1038 #endif
1039
1040         prop = of_get_property(node, "nvidia,num-temp-sensors", NULL);
1041         if (prop)
1042                 pdata.num_temp_sensors = be32_to_cpup(prop);
1043
1044         /* Configure input GPIOs */
1045         gpio = of_get_named_gpio(node, "nvidia,wake-gpio", 0);
1046         if (gpio == -EPROBE_DEFER) {
1047                 ret = -EPROBE_DEFER;
1048                 goto error;
1049         }
1050         pdata.wake_gpio = gpio_is_valid(gpio) ? gpio : -1;
1051         dev_info(&pdev->dev, "set MDM_WAKE_AP gpio:%d\n", pdata.wake_gpio);
1052         if (gpio_is_valid(gpio)) {
1053                 ret = devm_gpio_request(&pdev->dev, gpio, "MDM_WAKE_AP");
1054                 if (ret) {
1055                         dev_err(&pdev->dev, "request gpio %d failed\n", gpio);
1056                         goto error;
1057                 }
1058                 gpio_direction_input(gpio);
1059         }
1060
1061         gpio = of_get_named_gpio(node, "nvidia,boot-gpio", 0);
1062         if (gpio == -EPROBE_DEFER) {
1063                 ret = -EPROBE_DEFER;
1064                 goto error;
1065         }
1066         pdata.boot_gpio = gpio_is_valid(gpio) ? gpio : -1;
1067         dev_info(&pdev->dev, "set MDM_COLDBOOT gpio:%d\n", pdata.boot_gpio);
1068         if (gpio_is_valid(gpio)) {
1069                 ret = devm_gpio_request(&pdev->dev, gpio, "MDM_COLDBOOT");
1070                 if (ret) {
1071                         dev_err(&pdev->dev, "request gpio %d failed\n", gpio);
1072                         goto error;
1073                 }
1074                 gpio_direction_input(gpio);
1075         }
1076
1077         gpio = of_get_named_gpio(node, "nvidia,mdm-power-report-gpio", 0);
1078         if (gpio == -EPROBE_DEFER) {
1079                 ret = -EPROBE_DEFER;
1080                 goto error;
1081         }
1082         pdata.mdm_power_report_gpio = gpio_is_valid(gpio) ? gpio : -1;
1083         dev_info(&pdev->dev, "set MDM_PWR_REPORT gpio:%d\n",
1084                 pdata.mdm_power_report_gpio);
1085
1086         gpio = of_get_named_gpio(node, "nvidia,pre-boost-gpio", 0);
1087         if (gpio == -EPROBE_DEFER) {
1088                 ret = -EPROBE_DEFER;
1089                 goto error;
1090         }
1091         if (gpio_is_valid(gpio)) {
1092                 dev_info(&pdev->dev, "set pre boost gpio (%d) to 1\n", gpio);
1093                 ret = devm_gpio_request(&pdev->dev, gpio, "MODEM PREBOOST");
1094                 if (ret) {
1095                         dev_err(&pdev->dev, "request gpio %d failed\n", gpio);
1096                         goto error;
1097                 }
1098                 gpio_direction_output(gpio, 1);
1099                 modem->pre_boost_gpio = gpio;
1100         } else
1101                 modem->pre_boost_gpio = -1;
1102
1103         /* set GPIO IRQ flags */
1104         pdata.wake_irq_flags = pdata.boot_irq_flags =
1105                 pdata.mdm_power_irq_flags =
1106                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
1107
1108         /* initialize necessary output GPIO and start modem here */
1109         gpio = of_get_named_gpio(node, "nvidia,mdm-en-gpio", 0);
1110         if (gpio == -EPROBE_DEFER) {
1111                 ret = -EPROBE_DEFER;
1112                 goto error;
1113         }
1114         if (gpio_is_valid(gpio)) {
1115                 dev_info(&pdev->dev, "set MODEM EN (%d) to 1\n", gpio);
1116                 ret = devm_gpio_request(&pdev->dev, gpio, "MODEM EN");
1117                 if (ret) {
1118                         dev_err(&pdev->dev, "request gpio %d failed\n", gpio);
1119                         goto error;
1120                 }
1121                 gpio_direction_output(gpio, 1);
1122         }
1123
1124         gpio = of_get_named_gpio(node, "nvidia,reset-gpio", 0);
1125         if (gpio == -EPROBE_DEFER) {
1126                 ret = -EPROBE_DEFER;
1127                 goto error;
1128         }
1129         if (gpio_is_valid(gpio)) {
1130                 ret = devm_gpio_request(&pdev->dev, gpio, "MODEM RESET");
1131                 if (ret) {
1132                         dev_err(&pdev->dev, "request gpio %d failed\n", gpio);
1133                         goto error;
1134                 }
1135                 /* boot modem now */
1136                 /* Modem requires at least 10ms between MDM_EN assertion
1137                 and release of the reset. 20ms is the min value of msleep */
1138                 msleep(20);
1139                 /* Release modem reset to start boot */
1140                 dev_info(&pdev->dev, "set MODEM RESET (%d) to 1\n", gpio);
1141                 gpio_direction_output(gpio, 1);
1142                 gpio_export(gpio, false);
1143
1144                 /* also create a dedicated sysfs for the modem-reset gpio */
1145                 gpio_export_link(&pdev->dev, "modem_reset", gpio);
1146         }
1147
1148         /* add platform data to pdev */
1149         platform_device_add_data(pdev, &pdata, sizeof(pdata));
1150         return 0;
1151
1152 error:
1153         if (modem->regulator)
1154                 regulator_disable(modem->regulator);
1155         return ret;
1156 }
1157
1158 static int tegra_usb_modem_probe(struct platform_device *pdev)
1159 {
1160         struct tegra_usb_modem *modem;
1161         int ret = 0;
1162
1163         modem = kzalloc(sizeof(struct tegra_usb_modem), GFP_KERNEL);
1164         if (!modem) {
1165                 dev_dbg(&pdev->dev, "failed to allocate memory\n");
1166                 return -ENOMEM;
1167         }
1168
1169         /* initialize from device tree */
1170         ret = tegra_usb_modem_parse_dt(modem, pdev);
1171         if (ret) {
1172                 dev_err(&pdev->dev, "device tree parsing error\n");
1173                 kfree(modem);
1174                 return ret;
1175         }
1176
1177         ret = mdm_init(modem, pdev);
1178         if (ret) {
1179                 kfree(modem);
1180                 return ret;
1181         }
1182
1183         dev_set_drvdata(&pdev->dev, modem);
1184
1185         return ret;
1186 }
1187
1188 static int __exit tegra_usb_modem_remove(struct platform_device *pdev)
1189 {
1190         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1191
1192         unregister_pm_notifier(&modem->pm_notifier);
1193         usb_unregister_notify(&modem->usb_notifier);
1194
1195         if (modem->modem_edp_pdev)
1196                 platform_device_unregister(modem->modem_edp_pdev);
1197
1198         if (modem->wake_irq)
1199                 free_irq(modem->wake_irq, modem);
1200
1201         if (modem->boot_irq)
1202                 free_irq(modem->boot_irq, modem);
1203
1204         if (modem->sysfs_file_created)
1205                 device_remove_file(&pdev->dev, &dev_attr_load_host);
1206
1207         if (modem->modem_state_file_created)
1208                 device_remove_file(&pdev->dev, &dev_attr_modem_state);
1209
1210         if (!IS_ERR(modem->regulator))
1211                 regulator_put(modem->regulator);
1212
1213         cancel_delayed_work_sync(&modem->recovery_work);
1214         cancel_delayed_work_sync(&modem->enable_nvhsic_work);
1215         cancel_work_sync(&modem->host_load_work);
1216         cancel_work_sync(&modem->host_unload_work);
1217         cancel_work_sync(&modem->cpu_boost_work);
1218         cancel_delayed_work_sync(&modem->cpu_unboost_work);
1219         destroy_workqueue(modem->wq);
1220
1221         pm_qos_remove_request(&modem->cpu_boost_req);
1222
1223         kfree(modem);
1224         return 0;
1225 }
1226
1227 static void tegra_usb_modem_shutdown(struct platform_device *pdev)
1228 {
1229         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1230
1231         if (modem->ops && modem->ops->stop)
1232                 modem->ops->stop();
1233 }
1234
1235 #ifdef CONFIG_PM
1236 static int tegra_usb_modem_suspend(struct platform_device *pdev,
1237                                    pm_message_t state)
1238 {
1239         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1240         int ret = 0;
1241
1242         /* send L3 hint to modem */
1243         if (modem->ops && modem->ops->suspend)
1244                 modem->ops->suspend();
1245
1246         if (modem->wake_irq) {
1247                 ret = enable_irq_wake(modem->wake_irq);
1248                 if (ret) {
1249                         pr_info("%s, wake irq=%d, error=%d\n",
1250                                 __func__, modem->wake_irq, ret);
1251                         goto fail;
1252                 }
1253         }
1254
1255         if (modem->boot_irq) {
1256                 ret = enable_irq_wake(modem->boot_irq);
1257                 if (ret)
1258                         pr_info("%s, wake irq=%d, error=%d\n",
1259                                 __func__, modem->boot_irq, ret);
1260         }
1261
1262         if (gpio_is_valid(modem->pre_boost_gpio))
1263                 gpio_set_value(modem->pre_boost_gpio, 0);
1264 fail:
1265         return ret;
1266 }
1267
1268 static int tegra_usb_modem_resume(struct platform_device *pdev)
1269 {
1270         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1271         int ret = 0;
1272
1273         if (gpio_is_valid(modem->pre_boost_gpio))
1274                 gpio_set_value(modem->pre_boost_gpio, 1);
1275
1276         if (modem->boot_irq) {
1277                 ret = disable_irq_wake(modem->boot_irq);
1278                 if (ret)
1279                         pr_err("Failed to disable modem boot_irq\n");
1280         }
1281
1282         if (modem->wake_irq) {
1283                 ret = disable_irq_wake(modem->wake_irq);
1284                 if (ret)
1285                         pr_err("Failed to disable modem wake_irq\n");
1286         } else if (get_wakeup_reason_irq() == INT_USB2) {
1287                 pr_info("%s: remote wakeup from USB. Hold a timed wakelock\n",
1288                                 __func__);
1289                 wake_lock_timeout(&modem->wake_lock,
1290                                 WAKELOCK_TIMEOUT_FOR_REMOTE_WAKE);
1291         }
1292
1293         /* send L3->L0 hint to modem */
1294         if (modem->ops && modem->ops->resume)
1295                 modem->ops->resume();
1296         return 0;
1297 }
1298 #endif
1299
1300 static const struct of_device_id tegra_usb_modem_match[] = {
1301         { .compatible = "nvidia,icera-i500", },
1302         {},
1303 };
1304 MODULE_DEVICE_TABLE(of, tegra_usb_modem_match);
1305
1306 static struct platform_driver tegra_usb_modem_power_driver = {
1307         .driver = {
1308                    .name = "tegra_usb_modem_power",
1309                    .owner = THIS_MODULE,
1310                    .of_match_table = of_match_ptr(tegra_usb_modem_match),
1311                    },
1312         .probe = tegra_usb_modem_probe,
1313         .remove = __exit_p(tegra_usb_modem_remove),
1314         .shutdown = tegra_usb_modem_shutdown,
1315 #ifdef CONFIG_PM
1316         .suspend = tegra_usb_modem_suspend,
1317         .resume = tegra_usb_modem_resume,
1318 #endif
1319 };
1320
1321 static int __init tegra_usb_modem_power_init(void)
1322 {
1323         return platform_driver_register(&tegra_usb_modem_power_driver);
1324 }
1325
1326 module_init(tegra_usb_modem_power_init);
1327
1328 static void __exit tegra_usb_modem_power_exit(void)
1329 {
1330         platform_driver_unregister(&tegra_usb_modem_power_driver);
1331 }
1332
1333 module_exit(tegra_usb_modem_power_exit);
1334
1335 MODULE_DESCRIPTION("Tegra usb modem power management driver");
1336 MODULE_LICENSE("GPL");