misc: tegra-baseband: Fix NULL instead of integer
[linux-3.10.git] / drivers / misc / tegra-baseband / tegra_usb_modem_power.c
1 /*
2  * Copyright (c) 2011-2014, 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/dma-mapping.h>
40 #include <linux/tegra-pmc.h>
41 #include <linux/delay.h>
42 #include "../../../arch/arm/mach-tegra/iomap.h"
43 #include <linux/fs.h>
44 #include <asm/segment.h>
45 #include <asm/uaccess.h>
46 #include <linux/platform_data/modem_thermal.h>
47
48 #define BOOST_CPU_FREQ_MIN      1200000
49 #define BOOST_CPU_FREQ_TIMEOUT  5000
50
51 #define WAKELOCK_TIMEOUT_FOR_USB_ENUM           (HZ * 10)
52 #define WAKELOCK_TIMEOUT_FOR_REMOTE_WAKE        (HZ)
53
54 #define MAX_MODEM_EDP_STATES 10
55
56 /* default autosuspend and short autosuspend delay in ms */
57 #define DEFAULT_AUTOSUSPEND             2000
58 #define DEFAULT_SHORT_AUTOSUSPEND       50
59
60 #define XHCI_HSIC_POWER "/sys/bus/platform/devices/tegra-xhci/hsic0_power"
61
62 static void hsic_power(bool on)
63 {
64         struct file *fp = NULL;
65         mm_segment_t oldfs;
66         loff_t offset = 0;
67
68         oldfs = get_fs();
69         set_fs(get_ds());
70         fp = filp_open(XHCI_HSIC_POWER, O_RDWR, S_IRWXU);
71         if (IS_ERR(fp)) {
72                 pr_err("%s: error opening %s, error:%ld\n",
73                                 __func__, XHCI_HSIC_POWER, PTR_ERR(fp));
74                 return;
75         }
76
77         if (on)
78                 vfs_write(fp, "1", 1, &offset);
79         else
80                 vfs_write(fp, "0", 1, &offset);
81
82         vfs_fsync(fp, 0);
83         set_fs(oldfs);
84         filp_close(fp, NULL);
85 }
86
87 static u64 tegra_ehci_dmamask = DMA_BIT_MASK(64);
88
89 static struct resource tegra_usb2_resources[] = {
90         [0] = {
91                 .start  = TEGRA_USB2_BASE,
92                 .end    = TEGRA_USB2_BASE + TEGRA_USB2_SIZE - 1,
93                 .flags  = IORESOURCE_MEM,
94         },
95         [1] = {
96                 .start  = INT_USB2,
97                 .end    = INT_USB2,
98                 .flags  = IORESOURCE_IRQ,
99         },
100 };
101
102 static struct platform_device tegra_ehci2_device = {
103         .name   = "tegra-ehci",
104         .id     = 1,
105         .dev    = {
106                 .dma_mask       = &tegra_ehci_dmamask,
107                 .coherent_dma_mask = DMA_BIT_MASK(64),
108         },
109         .resource = tegra_usb2_resources,
110         .num_resources = ARRAY_SIZE(tegra_usb2_resources),
111 };
112
113 static struct tegra_usb_platform_data tegra_ehci2_hsic_baseband_pdata = {
114         .port_otg = false,
115         .has_hostpc = true,
116         .unaligned_dma_buf_supported = true,
117         .phy_intf = TEGRA_USB_PHY_INTF_HSIC,
118         .op_mode = TEGRA_USB_OPMODE_HOST,
119         .u_data.host = {
120                 .hot_plug = false,
121                 .remote_wakeup_supported = true,
122                 .power_off_on_suspend = true,
123         },
124 };
125
126 struct tegra_usb_modem {
127         struct tegra_usb_modem_power_platform_data *pdata;
128         struct platform_device *pdev;
129         struct regulator *regulator; /* modem power regulator */
130         unsigned int wake_cnt;  /* remote wakeup counter */
131         unsigned int wake_irq;  /* remote wakeup irq */
132         bool wake_irq_wakeable; /* LP0 wakeable */
133         unsigned int boot_irq;  /* modem boot irq */
134         bool boot_irq_wakeable; /* LP0 wakeable */
135         struct mutex lock;
136         struct wake_lock wake_lock;     /* modem wake lock */
137         unsigned int vid;       /* modem vendor id */
138         unsigned int pid;       /* modem product id */
139         struct usb_device *xusb_roothub;        /* XUSB roothub device */
140         struct usb_device *udev;        /* modem usb device */
141         struct usb_device *parent;      /* parent device */
142         struct usb_interface *intf;     /* first modem usb interface */
143         struct workqueue_struct *wq;    /* modem workqueue */
144         struct delayed_work recovery_work;      /* modem recovery work */
145         struct work_struct host_load_work;      /* usb host load work */
146         struct work_struct host_unload_work;    /* usb host unload work */
147         struct pm_qos_request cpu_boost_req; /* min CPU freq request */
148         struct work_struct cpu_boost_work;      /* CPU freq boost work */
149         struct delayed_work cpu_unboost_work;   /* CPU freq unboost work */
150         const struct tegra_modem_operations *ops;       /* modem operations */
151         unsigned int capability;        /* modem capability */
152         int system_suspend;     /* system suspend flag */
153         struct notifier_block pm_notifier;      /* pm event notifier */
154         struct notifier_block usb_notifier;     /* usb event notifier */
155         int sysfs_file_created;
156         int short_autosuspend_enabled;
157         struct platform_device *hc;     /* USB host controller */
158         struct mutex hc_lock;
159         struct mutex sysedp_lock;
160         unsigned int mdm_power_irq;     /* modem power increase irq */
161         bool mdm_power_irq_wakeable;    /* not used for LP0 wakeup */
162         int sysedp_prev_request;        /* previous modem request */
163         int sysedp_file_created;        /* sysedp state file created */
164         bool use_xhci_hsic;             /* indicate if we use XHCI HSIC */
165         struct platform_device *modem_thermal_pdev;
166 };
167
168
169 /* supported modems */
170 static const struct usb_device_id modem_list[] = {
171         {USB_DEVICE(0x1983, 0x0310),    /* Icera 450 rev1 */
172          .driver_info = TEGRA_MODEM_AUTOSUSPEND,
173          },
174         {USB_DEVICE(0x1983, 0x0321),    /* Icera 450 rev2 */
175          .driver_info = TEGRA_MODEM_AUTOSUSPEND,
176          },
177         {USB_DEVICE(0x1983, 0x0327),    /* Icera 450 5AE */
178          .driver_info = TEGRA_MODEM_AUTOSUSPEND,
179          },
180         {USB_DEVICE(0x1983, 0x0427),    /* Icera 500 5AN */
181          .driver_info = TEGRA_MODEM_AUTOSUSPEND,
182          },
183         {USB_DEVICE(0x1983, 0x1005),    /* Icera 500 5AN (BSD) */
184         /* .driver_info = TEGRA_MODEM_AUTOSUSPEND, */
185          },
186         {USB_DEVICE(0x1983, 0x1007),    /* Icera 500 Bruce */
187          .driver_info = TEGRA_USB_HOST_RELOAD|TEGRA_MODEM_AUTOSUSPEND,
188          },
189         {}
190 };
191
192 static ssize_t load_unload_usb_host(struct device *dev,
193                                     struct device_attribute *attr,
194                                     const char *buf, size_t count);
195
196 static void cpu_freq_unboost(struct work_struct *ws)
197 {
198         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
199                                                      cpu_unboost_work.work);
200
201         pm_qos_update_request(&modem->cpu_boost_req, PM_QOS_DEFAULT_VALUE);
202 }
203
204 static void cpu_freq_boost(struct work_struct *ws)
205 {
206         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
207                                                      cpu_boost_work);
208
209         cancel_delayed_work_sync(&modem->cpu_unboost_work);
210         pm_qos_update_request(&modem->cpu_boost_req, BOOST_CPU_FREQ_MIN);
211         queue_delayed_work(modem->wq, &modem->cpu_unboost_work,
212                               msecs_to_jiffies(BOOST_CPU_FREQ_TIMEOUT));
213 }
214
215 static irqreturn_t tegra_usb_modem_wake_thread(int irq, void *data)
216 {
217         struct tegra_usb_modem *modem = (struct tegra_usb_modem *)data;
218
219         mutex_lock(&modem->lock);
220         if (modem->udev && modem->udev->state != USB_STATE_NOTATTACHED) {
221                 wake_lock_timeout(&modem->wake_lock,
222                                   WAKELOCK_TIMEOUT_FOR_REMOTE_WAKE);
223
224                 dev_info(&modem->pdev->dev, "remote wake (%u)\n",
225                          ++(modem->wake_cnt));
226
227                 if (!modem->system_suspend) {
228                         usb_lock_device(modem->udev);
229                         if (usb_autopm_get_interface(modem->intf) == 0)
230                                 usb_autopm_put_interface_async(modem->intf);
231                         usb_unlock_device(modem->udev);
232                 }
233 #ifdef CONFIG_PM
234                 if (modem->capability & TEGRA_MODEM_AUTOSUSPEND &&
235                     modem->short_autosuspend_enabled) {
236                         pm_runtime_set_autosuspend_delay(&modem->udev->dev,
237                                         modem->pdata->autosuspend_delay);
238                         modem->short_autosuspend_enabled = 0;
239                 }
240 #endif
241         }
242         mutex_unlock(&modem->lock);
243
244         return IRQ_HANDLED;
245 }
246 static irqreturn_t tegra_usb_modem_sysedp_thread(int irq, void *data)
247 {
248         struct tegra_usb_modem *modem = (struct tegra_usb_modem *)data;
249
250         mutex_lock(&modem->sysedp_lock);
251         if (gpio_get_value(modem->pdata->mdm_power_report_gpio))
252                 sysedp_set_state_by_name("icemdm", 0);
253         else
254                 sysedp_set_state_by_name("icemdm", modem->sysedp_prev_request);
255         mutex_unlock(&modem->sysedp_lock);
256
257         return IRQ_HANDLED;
258 }
259
260 static ssize_t store_sysedp_state(struct device *dev,
261                                struct device_attribute *attr,
262                                const char *buf, size_t count)
263 {
264         struct tegra_usb_modem *modem = dev_get_drvdata(dev);
265         int state;
266
267         if (sscanf(buf, "%d", &state) != 1)
268                 return -EINVAL;
269
270         mutex_lock(&modem->sysedp_lock);
271         /*
272          * If the GPIO is low we set the state, otherwise the threaded
273          * interrupt handler will set it.
274          */
275         if (!gpio_get_value(modem->pdata->mdm_power_report_gpio))
276                 sysedp_set_state_by_name("icemdm", state);
277
278         /* store state for threaded interrupt handler */
279         modem->sysedp_prev_request = state;
280         mutex_unlock(&modem->sysedp_lock);
281
282         return count;
283 }
284 static DEVICE_ATTR(sysedp_state, S_IWUSR, NULL, store_sysedp_state);
285
286 static irqreturn_t tegra_usb_modem_boot_thread(int irq, void *data)
287 {
288         struct tegra_usb_modem *modem = (struct tegra_usb_modem *)data;
289         int v = gpio_get_value(modem->pdata->boot_gpio);
290
291         dev_info(&modem->pdev->dev, "MDM_COLDBOOT %s\n", v ? "high" : "low");
292         if (modem->capability & TEGRA_USB_HOST_RELOAD)
293                 if (!work_pending(&modem->host_load_work) &&
294                     !work_pending(&modem->host_unload_work))
295                         queue_work(modem->wq, v ? &modem->host_load_work :
296                                    &modem->host_unload_work);
297
298         /* hold wait lock to complete the enumeration */
299         wake_lock_timeout(&modem->wake_lock, WAKELOCK_TIMEOUT_FOR_USB_ENUM);
300
301         /* boost CPU freq */
302         if (!work_pending(&modem->cpu_boost_work))
303                 queue_work(modem->wq, &modem->cpu_boost_work);
304
305         /* USB disconnect maybe on going... */
306         mutex_lock(&modem->lock);
307         if (modem->udev && modem->udev->state != USB_STATE_NOTATTACHED)
308                 pr_warn("Device is not disconnected!\n");
309         mutex_unlock(&modem->lock);
310
311         return IRQ_HANDLED;
312 }
313
314 static void tegra_usb_modem_recovery(struct work_struct *ws)
315 {
316         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
317                                                      recovery_work.work);
318
319         mutex_lock(&modem->lock);
320         if (!modem->udev) {     /* assume modem crashed */
321                 if (modem->ops && modem->ops->reset)
322                         modem->ops->reset();
323         }
324         mutex_unlock(&modem->lock);
325 }
326
327 static void tegra_usb_host_load(struct work_struct *ws)
328 {
329         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
330                                                      host_load_work);
331         load_unload_usb_host(&modem->pdev->dev, NULL, "1", 1);
332 }
333
334 static void tegra_usb_host_unload(struct work_struct *ws)
335 {
336         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
337                                                      host_unload_work);
338         load_unload_usb_host(&modem->pdev->dev, NULL, "0", 1);
339 }
340
341 static void modem_device_add_handler(struct tegra_usb_modem *modem,
342                                struct usb_device *udev)
343 {
344         const struct usb_device_descriptor *desc = &udev->descriptor;
345         struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
346         const struct usb_device_id *id = NULL;
347
348         if (intf) {
349                 /* only look for specific modems if modem_list is provided in
350                    platform data. Otherwise, look for the modems in the default
351                    supported modem list */
352                 if (modem->pdata->modem_list)
353                         id = usb_match_id(intf, modem->pdata->modem_list);
354                 else
355                         id = usb_match_id(intf, modem_list);
356         }
357
358         if (id) {
359                 /* hold wakelock to ensure ril has enough time to restart */
360                 wake_lock_timeout(&modem->wake_lock,
361                                   WAKELOCK_TIMEOUT_FOR_USB_ENUM);
362 #ifdef CONFIG_PM
363                 if (modem->use_xhci_hsic)
364                         usb_enable_autosuspend(modem->xusb_roothub);
365 #endif
366
367                 pr_info("Add device %d <%s %s>\n", udev->devnum,
368                         udev->manufacturer, udev->product);
369
370                 mutex_lock(&modem->lock);
371                 modem->udev = udev;
372                 modem->parent = udev->parent;
373                 modem->intf = intf;
374                 modem->vid = desc->idVendor;
375                 modem->pid = desc->idProduct;
376                 modem->wake_cnt = 0;
377                 modem->capability = id->driver_info;
378                 mutex_unlock(&modem->lock);
379
380                 pr_info("persist_enabled: %u\n", udev->persist_enabled);
381
382 #ifdef CONFIG_PM
383                 if (modem->capability & TEGRA_MODEM_AUTOSUSPEND) {
384                         pm_runtime_set_autosuspend_delay(&udev->dev,
385                                         modem->pdata->autosuspend_delay);
386                         modem->short_autosuspend_enabled = 0;
387                         usb_enable_autosuspend(udev);
388                         pr_info("enable autosuspend for %s %s\n",
389                                 udev->manufacturer, udev->product);
390                 }
391
392                 /* allow the device to wake up the system */
393                 if (udev->actconfig->desc.bmAttributes &
394                     USB_CONFIG_ATT_WAKEUP)
395                         device_set_wakeup_enable(&udev->dev, true);
396
397 #endif
398         }
399 }
400
401 static void modem_device_remove_handler(struct tegra_usb_modem *modem,
402                                   struct usb_device *udev)
403 {
404         const struct usb_device_descriptor *desc = &udev->descriptor;
405
406         if (desc->idVendor == modem->vid && desc->idProduct == modem->pid) {
407                 pr_info("Remove device %d <%s %s>\n", udev->devnum,
408                         udev->manufacturer, udev->product);
409
410 #ifdef CONFIG_PM
411                 if (modem->use_xhci_hsic)
412                         usb_enable_autosuspend(modem->xusb_roothub);
413 #endif
414
415                 mutex_lock(&modem->lock);
416                 modem->udev = NULL;
417                 modem->intf = NULL;
418                 modem->vid = 0;
419                 mutex_unlock(&modem->lock);
420
421                 if (modem->capability & TEGRA_MODEM_RECOVERY)
422                         queue_delayed_work(modem->wq,
423                                            &modem->recovery_work, HZ * 10);
424         }
425 }
426
427
428 static void xusb_usb2_roothub_add_handler(struct tegra_usb_modem *modem,
429                 struct usb_device *udev)
430 {
431         const struct usb_device_descriptor *desc = &udev->descriptor;
432
433         if (modem->use_xhci_hsic &&
434                 desc->idVendor == 0x1d6b && desc->idProduct == 0x2 &&
435                 !strcmp(udev->serial, "tegra-xhci")) {
436                 pr_info("Add device %d <%s %s>\n", udev->devnum,
437                         udev->manufacturer, udev->product);
438                 mutex_lock(&modem->lock);
439                 modem->xusb_roothub = udev;
440                 mutex_unlock(&modem->lock);
441                 pr_info("%s: XHCI ready, setting modem RESET(%d) to 1\n",
442                         __func__, modem->pdata->reset_gpio);
443                 gpio_direction_output(modem->pdata->reset_gpio, 1);
444         }
445 }
446
447 static void xusb_usb2_roothub_remove_handler(struct tegra_usb_modem *modem,
448                 struct usb_device *udev)
449 {
450         const struct usb_device_descriptor *desc = &udev->descriptor;
451
452         if (modem->use_xhci_hsic &&
453                 desc->idVendor == 0x1d6b && desc->idProduct == 0x2 &&
454                 !strcmp(udev->serial, "tegra-xhci")) {
455                 pr_info("Remove device %d <%s %s>\n", udev->devnum,
456                         udev->manufacturer, udev->product);
457                 mutex_lock(&modem->lock);
458                 modem->xusb_roothub = NULL;
459                 mutex_unlock(&modem->lock);
460         }
461 }
462
463 static int mdm_usb_notifier(struct notifier_block *notifier,
464                             unsigned long usb_event, void *udev)
465 {
466         struct tegra_usb_modem *modem =
467             container_of(notifier, struct tegra_usb_modem, usb_notifier);
468
469         switch (usb_event) {
470         case USB_DEVICE_ADD:
471                 modem_device_add_handler(modem, udev);
472                 xusb_usb2_roothub_add_handler(modem, udev);
473                 break;
474         case USB_DEVICE_REMOVE:
475                 modem_device_remove_handler(modem, udev);
476                 xusb_usb2_roothub_remove_handler(modem, udev);
477                 break;
478         }
479         return NOTIFY_OK;
480 }
481
482 static int mdm_pm_notifier(struct notifier_block *notifier,
483                            unsigned long pm_event, void *unused)
484 {
485         struct tegra_usb_modem *modem =
486             container_of(notifier, struct tegra_usb_modem, pm_notifier);
487
488         mutex_lock(&modem->lock);
489         if (!modem->udev) {
490                 mutex_unlock(&modem->lock);
491                 return NOTIFY_DONE;
492         }
493
494         pr_info("%s: event %ld\n", __func__, pm_event);
495         switch (pm_event) {
496         case PM_SUSPEND_PREPARE:
497                 if (wake_lock_active(&modem->wake_lock)) {
498                         pr_warn("%s: wakelock was active, aborting suspend\n",
499                                 __func__);
500                         mutex_unlock(&modem->lock);
501                         return NOTIFY_STOP;
502                 }
503
504                 modem->system_suspend = 1;
505 #ifdef CONFIG_PM
506                 if (modem->capability & TEGRA_MODEM_AUTOSUSPEND &&
507                     modem->udev &&
508                     modem->udev->state != USB_STATE_NOTATTACHED) {
509                         pm_runtime_set_autosuspend_delay(&modem->udev->dev,
510                                         modem->pdata->short_autosuspend_delay);
511                         modem->short_autosuspend_enabled = 1;
512                 }
513 #endif
514                 mutex_unlock(&modem->lock);
515                 return NOTIFY_OK;
516         case PM_POST_SUSPEND:
517                 modem->system_suspend = 0;
518                 mutex_unlock(&modem->lock);
519                 return NOTIFY_OK;
520         }
521
522         mutex_unlock(&modem->lock);
523         return NOTIFY_DONE;
524 }
525
526 static int mdm_request_irq(struct tegra_usb_modem *modem,
527                            irq_handler_t thread_fn,
528                            unsigned int irq_gpio,
529                            unsigned long irq_flags,
530                            const char *label,
531                            unsigned int *irq,
532                            bool *is_wakeable)
533 {
534         int ret;
535
536         ret = gpio_request(irq_gpio, label);
537         if (ret)
538                 return ret;
539
540         /* enable IRQ for GPIO */
541         *irq = gpio_to_irq(irq_gpio);
542
543         /* request threaded irq for GPIO */
544         ret = request_threaded_irq(*irq, NULL, thread_fn, irq_flags, label,
545                                    modem);
546         if (ret) {
547                 *irq = 0;
548                 return ret;
549         }
550
551         ret = enable_irq_wake(*irq);
552         *is_wakeable = (ret) ? false : true;
553
554         return 0;
555 }
556
557 static void tegra_usb_modem_post_remote_wakeup(void)
558 {
559         struct device *dev;
560         struct tegra_usb_modem *modem;
561
562         dev = bus_find_device_by_name(&platform_bus_type, NULL,
563                                         "MDM");
564         if (!dev) {
565                 pr_warn("%s unable to find device name\n", __func__);
566                 return;
567         }
568
569         modem = dev_get_drvdata(dev);
570
571         mutex_lock(&modem->lock);
572 #ifdef CONFIG_PM
573         if (modem->capability & TEGRA_MODEM_AUTOSUSPEND &&
574             modem->udev &&
575             modem->udev->state != USB_STATE_NOTATTACHED &&
576             modem->short_autosuspend_enabled) {
577                 pm_runtime_set_autosuspend_delay(&modem->udev->dev,
578                                 modem->pdata->autosuspend_delay);
579                 modem->short_autosuspend_enabled = 0;
580         }
581 #endif
582         wake_lock_timeout(&modem->wake_lock, WAKELOCK_TIMEOUT_FOR_REMOTE_WAKE);
583         mutex_unlock(&modem->lock);
584
585         return;
586 }
587
588 /* load USB host controller */
589 static struct platform_device *tegra_usb_host_register(
590                                 const struct tegra_usb_modem *modem)
591 {
592         const struct platform_device *hc_device =
593             modem->pdata->tegra_ehci_device;
594         struct platform_device *pdev;
595         int val;
596
597         pdev = platform_device_alloc(hc_device->name, hc_device->id);
598         if (!pdev)
599                 return NULL;
600
601         val = platform_device_add_resources(pdev, hc_device->resource,
602                                             hc_device->num_resources);
603         if (val)
604                 goto error;
605
606         pdev->dev.dma_mask = hc_device->dev.dma_mask;
607         pdev->dev.coherent_dma_mask = hc_device->dev.coherent_dma_mask;
608
609         val = platform_device_add_data(pdev, modem->pdata->tegra_ehci_pdata,
610                                         sizeof(struct tegra_usb_platform_data));
611         if (val)
612                 goto error;
613
614         val = platform_device_add(pdev);
615         if (val)
616                 goto error;
617
618         return pdev;
619
620 error:
621         pr_err("%s: err %d\n", __func__, val);
622         platform_device_put(pdev);
623         return NULL;
624 }
625
626 /* unload USB host controller */
627 static void tegra_usb_host_unregister(struct platform_device *pdev)
628 {
629         platform_device_unregister(pdev);
630 }
631
632 static ssize_t show_usb_host(struct device *dev,
633                              struct device_attribute *attr, char *buf)
634 {
635         struct tegra_usb_modem *modem = dev_get_drvdata(dev);
636
637         return sprintf(buf, "%d\n", (modem->hc) ? 1 : 0);
638 }
639
640 static ssize_t load_unload_usb_host(struct device *dev,
641                                     struct device_attribute *attr,
642                                     const char *buf, size_t count)
643 {
644         struct tegra_usb_modem *modem = dev_get_drvdata(dev);
645         int host;
646
647         if (sscanf(buf, "%d", &host) != 1 || host < 0 || host > 1)
648                 return -EINVAL;
649
650
651         mutex_lock(&modem->hc_lock);
652         if (host) {
653                 if (modem->use_xhci_hsic) {
654                         /* XHCI */
655                         pr_info("Enable XHCI HSIC\n");
656 #ifdef CONFIG_PM
657                         if (modem->xusb_roothub)
658                                 usb_disable_autosuspend(modem->xusb_roothub);
659 #endif
660                         hsic_power(1);
661                         modem->hc = (struct platform_device *)1;
662                 } else {
663                         /* EHCI */
664                         pr_info("Load EHCI\n");
665                         if (!modem->hc)
666                                 modem->hc = tegra_usb_host_register(modem);
667                 }
668         } else {
669                 if (modem->use_xhci_hsic) {
670                         /* XHCI */
671                         pr_info("Disable XHCI HSIC\n");
672 #ifdef CONFIG_PM
673                         if (modem->xusb_roothub)
674                                 usb_disable_autosuspend(modem->xusb_roothub);
675 #endif
676                         hsic_power(0);
677                         modem->hc = NULL;
678                 } else {
679                         /* EHCI */
680                         pr_info("Unload EHCI\n");
681                         if (modem->hc) {
682                                 tegra_usb_host_unregister(modem->hc);
683                                 modem->hc = NULL;
684                         }
685                 }
686         }
687         mutex_unlock(&modem->hc_lock);
688
689         return count;
690 }
691
692 static DEVICE_ATTR(load_host, S_IRUSR | S_IWUSR, show_usb_host,
693                    load_unload_usb_host);
694
695 static struct tegra_usb_phy_platform_ops tegra_usb_modem_platform_ops = {
696         .post_remote_wakeup = tegra_usb_modem_post_remote_wakeup,
697 };
698
699 static struct modem_thermal_platform_data thermdata = {
700         .num_zones = 0,
701 };
702
703 static struct platform_device modem_thermal_device = {
704         .name = "modem-thermal",
705         .id = -1,
706         .num_resources = 0,
707         .dev = {
708                 .platform_data = NULL,
709         },
710 };
711
712 static int mdm_init(struct tegra_usb_modem *modem, struct platform_device *pdev)
713 {
714         struct tegra_usb_modem_power_platform_data *pdata =
715             pdev->dev.platform_data;
716         int ret = 0;
717
718         modem->pdata = pdata;
719         modem->pdev = pdev;
720
721         /* WAR to load statically defined ehci device/pdata */
722         pdata->tegra_ehci_device = &tegra_ehci2_device;
723         pdata->tegra_ehci_pdata = &tegra_ehci2_hsic_baseband_pdata;
724
725         pdata->autosuspend_delay = DEFAULT_AUTOSUSPEND;
726         pdata->short_autosuspend_delay = DEFAULT_SHORT_AUTOSUSPEND;
727
728         /* Clear PWR_DET bit */
729         pwr_detect_bit_write(GPIO_PWR_DET, false);
730
731         /* get modem operations from platform data */
732         modem->ops = (const struct tegra_modem_operations *)pdata->ops;
733
734         /* modem init */
735         if (modem->ops && modem->ops->init) {
736                 ret = modem->ops->init();
737                 if (ret)
738                         goto error;
739         }
740
741         mutex_init(&(modem->lock));
742         mutex_init(&modem->hc_lock);
743         wake_lock_init(&modem->wake_lock, WAKE_LOCK_SUSPEND, "mdm_lock");
744
745         /* if wake gpio is not specified we rely on native usb remote wake */
746         if (gpio_is_valid(pdata->wake_gpio)) {
747                 /* request remote wakeup irq from platform data */
748                 ret = mdm_request_irq(modem,
749                                       tegra_usb_modem_wake_thread,
750                                       pdata->wake_gpio,
751                                       pdata->wake_irq_flags,
752                                       "mdm_wake",
753                                       &modem->wake_irq,
754                                       &modem->wake_irq_wakeable);
755                 if (ret) {
756                         dev_err(&pdev->dev, "request wake irq error\n");
757                         goto error;
758                 }
759         } else {
760                 modem->pdata->tegra_ehci_pdata->ops =
761                                                 &tegra_usb_modem_platform_ops;
762         }
763
764         if (gpio_is_valid(pdata->boot_gpio)) {
765                 /* request boot irq from platform data */
766                 ret = mdm_request_irq(modem,
767                                       tegra_usb_modem_boot_thread,
768                                       pdata->boot_gpio,
769                                       pdata->boot_irq_flags,
770                                       "mdm_boot",
771                                       &modem->boot_irq,
772                                       &modem->boot_irq_wakeable);
773                 if (ret) {
774                         dev_err(&pdev->dev, "request boot irq error\n");
775                         goto error;
776                 }
777         } else
778                 dev_warn(&pdev->dev, "boot irq not specified\n");
779
780         if (gpio_is_valid(pdata->mdm_power_report_gpio)) {
781                 ret = mdm_request_irq(modem,
782                                       tegra_usb_modem_sysedp_thread,
783                                       pdata->mdm_power_report_gpio,
784                                       pdata->mdm_power_irq_flags,
785                                       "mdm_power_report",
786                                       &modem->mdm_power_irq,
787                                       &modem->mdm_power_irq_wakeable);
788                 if (ret) {
789                         dev_err(&pdev->dev, "request power irq error\n");
790                         goto error;
791                 }
792
793                 disable_irq_wake(modem->mdm_power_irq);
794                 modem->mdm_power_irq_wakeable = false;
795
796                 ret = device_create_file(&pdev->dev, &dev_attr_sysedp_state);
797                 if (ret) {
798                         dev_err(&pdev->dev, "can't create edp sysfs file\n");
799                         goto error;
800                 }
801                 modem->sysedp_file_created = 1;
802                 mutex_init(&modem->sysedp_lock);
803         }
804
805         /* create sysfs node to load/unload host controller */
806         ret = device_create_file(&pdev->dev, &dev_attr_load_host);
807         if (ret) {
808                 dev_err(&pdev->dev, "can't create sysfs file\n");
809                 goto error;
810         }
811         modem->sysfs_file_created = 1;
812         modem->capability = TEGRA_USB_HOST_RELOAD;
813         if (pdev->id >= 0)
814                 dev_set_name(&pdev->dev, "MDM%d", pdev->id);
815         else
816                 dev_set_name(&pdev->dev, "MDM");
817
818         /* create work queue platform_driver_registe */
819         modem->wq = create_workqueue("tegra_usb_mdm_queue");
820         INIT_DELAYED_WORK(&modem->recovery_work, tegra_usb_modem_recovery);
821         INIT_WORK(&modem->host_load_work, tegra_usb_host_load);
822         INIT_WORK(&modem->host_unload_work, tegra_usb_host_unload);
823         INIT_WORK(&modem->cpu_boost_work, cpu_freq_boost);
824         INIT_DELAYED_WORK(&modem->cpu_unboost_work, cpu_freq_unboost);
825
826         pm_qos_add_request(&modem->cpu_boost_req, PM_QOS_CPU_FREQ_MIN,
827                            PM_QOS_DEFAULT_VALUE);
828
829         modem->pm_notifier.notifier_call = mdm_pm_notifier;
830         modem->usb_notifier.notifier_call = mdm_usb_notifier;
831
832         usb_register_notify(&modem->usb_notifier);
833         register_pm_notifier(&modem->pm_notifier);
834
835         if (pdata->num_temp_sensors) {
836                 thermdata.num_zones = pdata->num_temp_sensors;
837                 modem_thermal_device.dev.platform_data = &thermdata;
838                 modem->modem_thermal_pdev = &modem_thermal_device;
839                 platform_device_register(modem->modem_thermal_pdev);
840         }
841
842         /* start modem */
843         if (modem->ops && modem->ops->start)
844                 modem->ops->start();
845
846         return ret;
847 error:
848         if (modem->sysfs_file_created)
849                 device_remove_file(&pdev->dev, &dev_attr_load_host);
850
851         if (modem->wake_irq)
852                 free_irq(modem->wake_irq, modem);
853
854         if (modem->boot_irq)
855                 free_irq(modem->boot_irq, modem);
856
857         if (modem->sysedp_file_created)
858                 device_remove_file(&pdev->dev, &dev_attr_sysedp_state);
859
860         if (modem->mdm_power_irq)
861                 free_irq(modem->mdm_power_irq, modem);
862
863         return ret;
864 }
865
866 static int tegra_usb_modem_parse_dt(struct tegra_usb_modem *modem,
867                 struct platform_device *pdev)
868 {
869         struct tegra_usb_modem_power_platform_data *pdata =
870                 pdev->dev.platform_data;
871         struct device_node *node = pdev->dev.of_node;
872         const unsigned int *prop;
873         int gpio;
874         int ret;
875         u32 use_xhci = 0;
876
877         if (!node)
878                 return 0;
879
880         dev_dbg(&pdev->dev, "read platform data from DT\n");
881
882         /* allocate platform data if necessary */
883         if (!pdata) {
884                 pdata = devm_kzalloc(&pdev->dev,
885                         sizeof(struct tegra_usb_modem_power_platform_data),
886                         GFP_KERNEL);
887                 if (!pdata) {
888                         dev_warn(&pdev->dev, "failed to allocate memory\n");
889                         return -ENOMEM;
890                 }
891                 pdev->dev.platform_data = pdata;
892         }
893
894         /* turn on modem regulator if required */
895         pdata->regulator_name = of_get_property(node, "nvidia,regulator", NULL);
896         if (pdata->regulator_name) {
897                 modem->regulator = regulator_get(&pdev->dev,
898                                 pdata->regulator_name);
899                 if (IS_ERR(modem->regulator)) {
900                         dev_err(&pdev->dev, "failed to get regulator %s\n",
901                                 pdata->regulator_name);
902                         return PTR_ERR(modem->regulator);
903                 }
904                 ret = regulator_enable(modem->regulator);
905                 if (ret) {
906                         dev_err(&pdev->dev, "failed to enable regulator %s\n",
907                                 pdata->regulator_name);
908                         regulator_put(modem->regulator);
909                         return ret;
910                 }
911         }
912
913         /* determine if we are using EHCI or XHCI HSIC */
914         ret = of_property_read_u32(node, "nvidia,use-xhci-hsic", &use_xhci);
915         modem->use_xhci_hsic = (ret == 0 && use_xhci) ? 1 : 0;
916         dev_info(&pdev->dev, "using %s HSIC\n",
917                 modem->use_xhci_hsic ? "XHCI" : "EHCI");
918
919         prop = of_get_property(node, "nvidia,num-temp-sensors", NULL);
920         if (prop)
921                 pdata->num_temp_sensors = be32_to_cpup(prop);
922
923         /* GPIO */
924         gpio = of_get_named_gpio(node, "nvidia,wake-gpio", 0);
925         pdata->wake_gpio = gpio_is_valid(gpio) ? gpio : -1;
926         dev_info(&pdev->dev, "set wake gpio:%d\n", pdata->wake_gpio);
927
928         gpio = of_get_named_gpio(node, "nvidia,boot-gpio", 0);
929         pdata->boot_gpio = gpio_is_valid(gpio) ? gpio : -1;
930         dev_info(&pdev->dev, "set boot gpio:%d\n", pdata->boot_gpio);
931
932         gpio = of_get_named_gpio(node, "nvidia,mdm-power-report-gpio", 0);
933         pdata->mdm_power_report_gpio = gpio_is_valid(gpio) ? gpio : -1;
934         dev_info(&pdev->dev, "set mdm power report gpio:%d\n",
935                 pdata->mdm_power_report_gpio);
936
937         /* set GPIO IRQ flags */
938         pdata->wake_irq_flags = pdata->boot_irq_flags =
939                 pdata->mdm_power_irq_flags =
940                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
941
942         /* initialize necessary GPIO and start modem here */
943         gpio = of_get_named_gpio(node, "nvidia,mdm-en-gpio", 0);
944         if (gpio_is_valid(gpio)) {
945                 dev_info(&pdev->dev, "set MODEM EN (%d) to 1\n", gpio);
946                 ret = gpio_request(gpio, "MODEM EN");
947                 if (ret) {
948                         dev_err(&pdev->dev, "request gpio %d failed\n", gpio);
949                         return ret;
950                 }
951                 gpio_direction_output(gpio, 1);
952         }
953
954         gpio = of_get_named_gpio(node, "nvidia,mdm-sar0-gpio", 0);
955         if (gpio_is_valid(gpio)) {
956                 dev_info(&pdev->dev, "set MODEM SAR0 (%d) to 0\n", gpio);
957                 ret = gpio_request(gpio, "MODEM SAR0");
958                 if (ret) {
959                         dev_err(&pdev->dev, "request gpio %d failed\n", gpio);
960                         return ret;
961                 }
962                 gpio_direction_output(gpio, 0);
963                 gpio_export(gpio, false);
964         }
965
966         gpio = of_get_named_gpio(node, "nvidia,reset-gpio", 0);
967         if (gpio_is_valid(gpio)) {
968                 pdata->reset_gpio = gpio;
969                 ret = gpio_request(gpio, "MODEM RESET");
970                 if (ret) {
971                         dev_err(&pdev->dev, "request gpio %d failed\n", gpio);
972                         return ret;
973                 }
974                 /* boot modem now if EHCI is used */
975                 if (!modem->use_xhci_hsic) {
976                         /* Modem requires at least 10ms between MDM_EN assertion
977                         and release of the reset. 20ms is the min value of
978                         msleep */
979                         msleep(20);
980                         /* Release modem reset to start boot */
981                         dev_info(&pdev->dev, "set MODEM RESET (%d) to 1\n",
982                                  gpio);
983                         gpio_direction_output(gpio, 1);
984                 }
985                 gpio_export(gpio, false);
986         } else
987                 pdata->reset_gpio = -1;
988
989         return 0;
990 }
991
992 static int tegra_usb_modem_probe(struct platform_device *pdev)
993 {
994         struct tegra_usb_modem *modem;
995         int ret = 0;
996
997         modem = kzalloc(sizeof(struct tegra_usb_modem), GFP_KERNEL);
998         if (!modem) {
999                 dev_dbg(&pdev->dev, "failed to allocate memory\n");
1000                 return -ENOMEM;
1001         }
1002
1003         /* initialize from device tree */
1004         ret = tegra_usb_modem_parse_dt(modem, pdev);
1005         if (ret) {
1006                 dev_err(&pdev->dev, "device tree parsing error\n");
1007                 kfree(modem);
1008                 return ret;
1009         }
1010
1011         ret = mdm_init(modem, pdev);
1012         if (ret) {
1013                 kfree(modem);
1014                 return ret;
1015         }
1016
1017         dev_set_drvdata(&pdev->dev, modem);
1018
1019         return ret;
1020 }
1021
1022 static int __exit tegra_usb_modem_remove(struct platform_device *pdev)
1023 {
1024         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1025
1026         unregister_pm_notifier(&modem->pm_notifier);
1027         usb_unregister_notify(&modem->usb_notifier);
1028
1029         if (modem->wake_irq)
1030                 free_irq(modem->wake_irq, modem);
1031
1032         if (modem->boot_irq)
1033                 free_irq(modem->boot_irq, modem);
1034
1035         if (modem->mdm_power_irq)
1036                 free_irq(modem->mdm_power_irq, modem);
1037
1038         if (modem->sysedp_file_created)
1039                 device_remove_file(&pdev->dev, &dev_attr_sysedp_state);
1040
1041         if (modem->sysfs_file_created)
1042                 device_remove_file(&pdev->dev, &dev_attr_load_host);
1043
1044         if (!IS_ERR(modem->regulator))
1045                 regulator_put(modem->regulator);
1046
1047         cancel_delayed_work_sync(&modem->recovery_work);
1048         cancel_work_sync(&modem->host_load_work);
1049         cancel_work_sync(&modem->host_unload_work);
1050         cancel_work_sync(&modem->cpu_boost_work);
1051         cancel_delayed_work_sync(&modem->cpu_unboost_work);
1052         destroy_workqueue(modem->wq);
1053
1054         pm_qos_remove_request(&modem->cpu_boost_req);
1055
1056         kfree(modem);
1057         return 0;
1058 }
1059
1060 static void tegra_usb_modem_shutdown(struct platform_device *pdev)
1061 {
1062         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1063
1064         if (modem->ops && modem->ops->stop)
1065                 modem->ops->stop();
1066 }
1067
1068 #ifdef CONFIG_PM
1069 static int tegra_usb_modem_suspend(struct platform_device *pdev,
1070                                    pm_message_t state)
1071 {
1072         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1073         int ret = 0;
1074
1075         /* send L3 hint to modem */
1076         if (modem->ops && modem->ops->suspend)
1077                 modem->ops->suspend();
1078
1079         if (modem->wake_irq) {
1080                 ret = enable_irq_wake(modem->wake_irq);
1081                 if (ret) {
1082                         pr_info("%s, wake irq=%d, error=%d\n",
1083                                 __func__, modem->wake_irq, ret);
1084                         goto fail;
1085                 }
1086         }
1087
1088         if (modem->boot_irq) {
1089                 ret = enable_irq_wake(modem->boot_irq);
1090                 if (ret)
1091                         pr_info("%s, wake irq=%d, error=%d\n",
1092                                 __func__, modem->boot_irq, ret);
1093         }
1094 fail:
1095         return ret;
1096 }
1097
1098 static int tegra_usb_modem_resume(struct platform_device *pdev)
1099 {
1100         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1101         int ret = 0;
1102
1103         if (modem->boot_irq) {
1104                 ret = disable_irq_wake(modem->boot_irq);
1105                 if (ret)
1106                         pr_err("Failed to disable modem boot_irq\n");
1107         }
1108
1109         if (modem->wake_irq) {
1110                 ret = disable_irq_wake(modem->wake_irq);
1111                 if (ret)
1112                         pr_err("Failed to disable modem wake_irq\n");
1113         }
1114
1115         /* send L3->L0 hint to modem */
1116         if (modem->ops && modem->ops->resume)
1117                 modem->ops->resume();
1118         return 0;
1119 }
1120 #endif
1121
1122 static const struct of_device_id tegra_usb_modem_match[] = {
1123         { .compatible = "nvidia,icera-i500", },
1124         {},
1125 };
1126 MODULE_DEVICE_TABLE(of, tegra_usb_modem_match);
1127
1128 static struct platform_driver tegra_usb_modem_power_driver = {
1129         .driver = {
1130                    .name = "tegra_usb_modem_power",
1131                    .owner = THIS_MODULE,
1132                    .of_match_table = of_match_ptr(tegra_usb_modem_match),
1133                    },
1134         .probe = tegra_usb_modem_probe,
1135         .remove = __exit_p(tegra_usb_modem_remove),
1136         .shutdown = tegra_usb_modem_shutdown,
1137 #ifdef CONFIG_PM
1138         .suspend = tegra_usb_modem_suspend,
1139         .resume = tegra_usb_modem_resume,
1140 #endif
1141 };
1142
1143 static int __init tegra_usb_modem_power_init(void)
1144 {
1145         return platform_driver_register(&tegra_usb_modem_power_driver);
1146 }
1147
1148 module_init(tegra_usb_modem_power_init);
1149
1150 static void __exit tegra_usb_modem_power_exit(void)
1151 {
1152         platform_driver_unregister(&tegra_usb_modem_power_driver);
1153 }
1154
1155 module_exit(tegra_usb_modem_power_exit);
1156
1157 MODULE_DESCRIPTION("Tegra usb modem power management driver");
1158 MODULE_LICENSE("GPL");