]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - drivers/misc/tegra-baseband/tegra_usb_modem_power.c
drivers: thermal: modem support
[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 = 0,
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         /* if wake gpio is not specified we rely on native usb remote wake */
742         if (gpio_is_valid(pdata->wake_gpio)) {
743                 /* request remote wakeup irq from platform data */
744                 ret = mdm_request_irq(modem,
745                                       tegra_usb_modem_wake_thread,
746                                       pdata->wake_gpio,
747                                       pdata->wake_irq_flags,
748                                       "mdm_wake",
749                                       &modem->wake_irq,
750                                       &modem->wake_irq_wakeable);
751                 if (ret) {
752                         dev_err(&pdev->dev, "request wake irq error\n");
753                         goto error;
754                 }
755         } else {
756                 modem->pdata->tegra_ehci_pdata->ops =
757                                                 &tegra_usb_modem_platform_ops;
758         }
759
760         if (gpio_is_valid(pdata->boot_gpio)) {
761                 /* request boot irq from platform data */
762                 ret = mdm_request_irq(modem,
763                                       tegra_usb_modem_boot_thread,
764                                       pdata->boot_gpio,
765                                       pdata->boot_irq_flags,
766                                       "mdm_boot",
767                                       &modem->boot_irq,
768                                       &modem->boot_irq_wakeable);
769                 if (ret) {
770                         dev_err(&pdev->dev, "request boot irq error\n");
771                         goto error;
772                 }
773         } else
774                 dev_warn(&pdev->dev, "boot irq not specified\n");
775
776         if (gpio_is_valid(pdata->mdm_power_report_gpio)) {
777                 ret = mdm_request_irq(modem,
778                                       tegra_usb_modem_sysedp_thread,
779                                       pdata->mdm_power_report_gpio,
780                                       pdata->mdm_power_irq_flags,
781                                       "mdm_power_report",
782                                       &modem->mdm_power_irq,
783                                       &modem->mdm_power_irq_wakeable);
784                 if (ret) {
785                         dev_err(&pdev->dev, "request power irq error\n");
786                         goto error;
787                 }
788
789                 disable_irq_wake(modem->mdm_power_irq);
790                 modem->mdm_power_irq_wakeable = false;
791
792                 ret = device_create_file(&pdev->dev, &dev_attr_sysedp_state);
793                 if (ret) {
794                         dev_err(&pdev->dev, "can't create edp sysfs file\n");
795                         goto error;
796                 }
797                 modem->sysedp_file_created = 1;
798                 mutex_init(&modem->sysedp_lock);
799         }
800
801         /* create sysfs node to load/unload host controller */
802         ret = device_create_file(&pdev->dev, &dev_attr_load_host);
803         if (ret) {
804                 dev_err(&pdev->dev, "can't create sysfs file\n");
805                 goto error;
806         }
807         modem->sysfs_file_created = 1;
808         modem->capability = TEGRA_USB_HOST_RELOAD;
809         mutex_init(&(modem->lock));
810         mutex_init(&modem->hc_lock);
811         wake_lock_init(&modem->wake_lock, WAKE_LOCK_SUSPEND, "mdm_lock");
812         if (pdev->id >= 0)
813                 dev_set_name(&pdev->dev, "MDM%d", pdev->id);
814         else
815                 dev_set_name(&pdev->dev, "MDM");
816
817         /* create work queue platform_driver_registe */
818         modem->wq = create_workqueue("tegra_usb_mdm_queue");
819         INIT_DELAYED_WORK(&modem->recovery_work, tegra_usb_modem_recovery);
820         INIT_WORK(&modem->host_load_work, tegra_usb_host_load);
821         INIT_WORK(&modem->host_unload_work, tegra_usb_host_unload);
822         INIT_WORK(&modem->cpu_boost_work, cpu_freq_boost);
823         INIT_DELAYED_WORK(&modem->cpu_unboost_work, cpu_freq_unboost);
824
825         pm_qos_add_request(&modem->cpu_boost_req, PM_QOS_CPU_FREQ_MIN,
826                            PM_QOS_DEFAULT_VALUE);
827
828         modem->pm_notifier.notifier_call = mdm_pm_notifier;
829         modem->usb_notifier.notifier_call = mdm_usb_notifier;
830
831         usb_register_notify(&modem->usb_notifier);
832         register_pm_notifier(&modem->pm_notifier);
833
834         if (pdata->num_temp_sensors) {
835                 thermdata.num_zones = pdata->num_temp_sensors;
836                 modem_thermal_device.dev.platform_data = &thermdata;
837                 modem->modem_thermal_pdev = &modem_thermal_device;
838                 platform_device_register(modem->modem_thermal_pdev);
839         }
840
841         /* start modem */
842         if (modem->ops && modem->ops->start)
843                 modem->ops->start();
844
845         return ret;
846 error:
847         if (modem->sysfs_file_created)
848                 device_remove_file(&pdev->dev, &dev_attr_load_host);
849
850         if (modem->wake_irq)
851                 free_irq(modem->wake_irq, modem);
852
853         if (modem->boot_irq)
854                 free_irq(modem->boot_irq, modem);
855
856         if (modem->sysedp_file_created)
857                 device_remove_file(&pdev->dev, &dev_attr_sysedp_state);
858
859         if (modem->mdm_power_irq)
860                 free_irq(modem->mdm_power_irq, modem);
861
862         return ret;
863 }
864
865 static int tegra_usb_modem_parse_dt(struct tegra_usb_modem *modem,
866                 struct platform_device *pdev)
867 {
868         struct tegra_usb_modem_power_platform_data *pdata =
869                 pdev->dev.platform_data;
870         struct device_node *node = pdev->dev.of_node;
871         const unsigned int *prop;
872         int gpio;
873         int ret;
874         u32 use_xhci = 0;
875
876         if (!node)
877                 return 0;
878
879         dev_dbg(&pdev->dev, "read platform data from DT\n");
880
881         /* allocate platform data if necessary */
882         if (!pdata) {
883                 pdata = devm_kzalloc(&pdev->dev,
884                         sizeof(struct tegra_usb_modem_power_platform_data),
885                         GFP_KERNEL);
886                 if (!pdata) {
887                         dev_warn(&pdev->dev, "failed to allocate memory\n");
888                         return -ENOMEM;
889                 }
890                 pdev->dev.platform_data = pdata;
891         }
892
893         /* turn on modem regulator if required */
894         pdata->regulator_name = of_get_property(node, "nvidia,regulator", NULL);
895         if (pdata->regulator_name) {
896                 modem->regulator = regulator_get(&pdev->dev,
897                                 pdata->regulator_name);
898                 if (IS_ERR(modem->regulator)) {
899                         dev_err(&pdev->dev, "failed to get regulator %s\n",
900                                 pdata->regulator_name);
901                         return PTR_ERR(modem->regulator);
902                 }
903                 ret = regulator_enable(modem->regulator);
904                 if (ret) {
905                         dev_err(&pdev->dev, "failed to enable regulator %s\n",
906                                 pdata->regulator_name);
907                         regulator_put(modem->regulator);
908                         return ret;
909                 }
910         }
911
912         /* determine if we are using EHCI or XHCI HSIC */
913         ret = of_property_read_u32(node, "nvidia,use-xhci-hsic", &use_xhci);
914         modem->use_xhci_hsic = (ret == 0 && use_xhci) ? 1 : 0;
915         dev_info(&pdev->dev, "using %s HSIC\n",
916                 modem->use_xhci_hsic ? "XHCI" : "EHCI");
917
918         prop = of_get_property(node, "nvidia,num-temp-sensors", NULL);
919         if (prop)
920                 pdata->num_temp_sensors = be32_to_cpup(prop);
921
922         /* GPIO */
923         gpio = of_get_named_gpio(node, "nvidia,wake-gpio", 0);
924         pdata->wake_gpio = gpio_is_valid(gpio) ? gpio : -1;
925         dev_info(&pdev->dev, "set wake gpio:%d\n", pdata->wake_gpio);
926
927         gpio = of_get_named_gpio(node, "nvidia,boot-gpio", 0);
928         pdata->boot_gpio = gpio_is_valid(gpio) ? gpio : -1;
929         dev_info(&pdev->dev, "set boot gpio:%d\n", pdata->boot_gpio);
930
931         gpio = of_get_named_gpio(node, "nvidia,mdm-power-report-gpio", 0);
932         pdata->mdm_power_report_gpio = gpio_is_valid(gpio) ? gpio : -1;
933         dev_info(&pdev->dev, "set mdm power report gpio:%d\n",
934                 pdata->mdm_power_report_gpio);
935
936         /* set GPIO IRQ flags */
937         pdata->wake_irq_flags = pdata->boot_irq_flags =
938                 pdata->mdm_power_irq_flags =
939                 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
940
941         /* initialize necessary GPIO and start modem here */
942         gpio = of_get_named_gpio(node, "nvidia,mdm-en-gpio", 0);
943         if (gpio_is_valid(gpio)) {
944                 dev_info(&pdev->dev, "set MODEM EN (%d) to 1\n", gpio);
945                 ret = gpio_request(gpio, "MODEM EN");
946                 if (ret) {
947                         dev_err(&pdev->dev, "request gpio %d failed\n", gpio);
948                         return ret;
949                 }
950                 gpio_direction_output(gpio, 1);
951         }
952
953         gpio = of_get_named_gpio(node, "nvidia,mdm-sar0-gpio", 0);
954         if (gpio_is_valid(gpio)) {
955                 dev_info(&pdev->dev, "set MODEM SAR0 (%d) to 0\n", gpio);
956                 ret = gpio_request(gpio, "MODEM SAR0");
957                 if (ret) {
958                         dev_err(&pdev->dev, "request gpio %d failed\n", gpio);
959                         return ret;
960                 }
961                 gpio_direction_output(gpio, 0);
962                 gpio_export(gpio, false);
963         }
964
965         gpio = of_get_named_gpio(node, "nvidia,reset-gpio", 0);
966         if (gpio_is_valid(gpio)) {
967                 pdata->reset_gpio = gpio;
968                 ret = gpio_request(gpio, "MODEM RESET");
969                 if (ret) {
970                         dev_err(&pdev->dev, "request gpio %d failed\n", gpio);
971                         return ret;
972                 }
973                 /* boot modem now if EHCI is used */
974                 if (!modem->use_xhci_hsic) {
975                         /* Modem requires at least 10ms between MDM_EN assertion
976                         and release of the reset. 20ms is the min value of
977                         msleep */
978                         msleep(20);
979                         /* Release modem reset to start boot */
980                         dev_info(&pdev->dev, "set MODEM RESET (%d) to 1\n",
981                                  gpio);
982                         gpio_direction_output(gpio, 1);
983                 }
984                 gpio_export(gpio, false);
985         } else
986                 pdata->reset_gpio = -1;
987
988         return 0;
989 }
990
991 static int tegra_usb_modem_probe(struct platform_device *pdev)
992 {
993         struct tegra_usb_modem *modem;
994         int ret = 0;
995
996         modem = kzalloc(sizeof(struct tegra_usb_modem), GFP_KERNEL);
997         if (!modem) {
998                 dev_dbg(&pdev->dev, "failed to allocate memory\n");
999                 return -ENOMEM;
1000         }
1001
1002         /* initialize from device tree */
1003         ret = tegra_usb_modem_parse_dt(modem, pdev);
1004         if (ret) {
1005                 dev_err(&pdev->dev, "device tree parsing error\n");
1006                 kfree(modem);
1007                 return ret;
1008         }
1009
1010         ret = mdm_init(modem, pdev);
1011         if (ret) {
1012                 kfree(modem);
1013                 return ret;
1014         }
1015
1016         dev_set_drvdata(&pdev->dev, modem);
1017
1018         return ret;
1019 }
1020
1021 static int __exit tegra_usb_modem_remove(struct platform_device *pdev)
1022 {
1023         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1024
1025         unregister_pm_notifier(&modem->pm_notifier);
1026         usb_unregister_notify(&modem->usb_notifier);
1027
1028         if (modem->wake_irq)
1029                 free_irq(modem->wake_irq, modem);
1030
1031         if (modem->boot_irq)
1032                 free_irq(modem->boot_irq, modem);
1033
1034         if (modem->mdm_power_irq)
1035                 free_irq(modem->mdm_power_irq, modem);
1036
1037         if (modem->sysedp_file_created)
1038                 device_remove_file(&pdev->dev, &dev_attr_sysedp_state);
1039
1040         if (modem->sysfs_file_created)
1041                 device_remove_file(&pdev->dev, &dev_attr_load_host);
1042
1043         if (!IS_ERR(modem->regulator))
1044                 regulator_put(modem->regulator);
1045
1046         cancel_delayed_work_sync(&modem->recovery_work);
1047         cancel_work_sync(&modem->host_load_work);
1048         cancel_work_sync(&modem->host_unload_work);
1049         cancel_work_sync(&modem->cpu_boost_work);
1050         cancel_delayed_work_sync(&modem->cpu_unboost_work);
1051         destroy_workqueue(modem->wq);
1052
1053         pm_qos_remove_request(&modem->cpu_boost_req);
1054
1055         kfree(modem);
1056         return 0;
1057 }
1058
1059 static void tegra_usb_modem_shutdown(struct platform_device *pdev)
1060 {
1061         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1062
1063         if (modem->ops && modem->ops->stop)
1064                 modem->ops->stop();
1065 }
1066
1067 #ifdef CONFIG_PM
1068 static int tegra_usb_modem_suspend(struct platform_device *pdev,
1069                                    pm_message_t state)
1070 {
1071         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1072         int ret = 0;
1073
1074         /* send L3 hint to modem */
1075         if (modem->ops && modem->ops->suspend)
1076                 modem->ops->suspend();
1077
1078         if (modem->wake_irq) {
1079                 ret = enable_irq_wake(modem->wake_irq);
1080                 if (ret) {
1081                         pr_info("%s, wake irq=%d, error=%d\n",
1082                                 __func__, modem->wake_irq, ret);
1083                         goto fail;
1084                 }
1085         }
1086
1087         if (modem->boot_irq) {
1088                 ret = enable_irq_wake(modem->boot_irq);
1089                 if (ret)
1090                         pr_info("%s, wake irq=%d, error=%d\n",
1091                                 __func__, modem->boot_irq, ret);
1092         }
1093 fail:
1094         return ret;
1095 }
1096
1097 static int tegra_usb_modem_resume(struct platform_device *pdev)
1098 {
1099         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1100         int ret = 0;
1101
1102         if (modem->boot_irq) {
1103                 ret = disable_irq_wake(modem->boot_irq);
1104                 if (ret)
1105                         pr_err("Failed to disable modem boot_irq\n");
1106         }
1107
1108         if (modem->wake_irq) {
1109                 ret = disable_irq_wake(modem->wake_irq);
1110                 if (ret)
1111                         pr_err("Failed to disable modem wake_irq\n");
1112         }
1113
1114         /* send L3->L0 hint to modem */
1115         if (modem->ops && modem->ops->resume)
1116                 modem->ops->resume();
1117         return 0;
1118 }
1119 #endif
1120
1121 static const struct of_device_id tegra_usb_modem_match[] = {
1122         { .compatible = "nvidia,icera-i500", },
1123         {},
1124 };
1125 MODULE_DEVICE_TABLE(of, tegra_usb_modem_match);
1126
1127 static struct platform_driver tegra_usb_modem_power_driver = {
1128         .driver = {
1129                    .name = "tegra_usb_modem_power",
1130                    .owner = THIS_MODULE,
1131                    .of_match_table = of_match_ptr(tegra_usb_modem_match),
1132                    },
1133         .probe = tegra_usb_modem_probe,
1134         .remove = __exit_p(tegra_usb_modem_remove),
1135         .shutdown = tegra_usb_modem_shutdown,
1136 #ifdef CONFIG_PM
1137         .suspend = tegra_usb_modem_suspend,
1138         .resume = tegra_usb_modem_resume,
1139 #endif
1140 };
1141
1142 static int __init tegra_usb_modem_power_init(void)
1143 {
1144         return platform_driver_register(&tegra_usb_modem_power_driver);
1145 }
1146
1147 module_init(tegra_usb_modem_power_init);
1148
1149 static void __exit tegra_usb_modem_power_exit(void)
1150 {
1151         platform_driver_unregister(&tegra_usb_modem_power_driver);
1152 }
1153
1154 module_exit(tegra_usb_modem_power_exit);
1155
1156 MODULE_DESCRIPTION("Tegra usb modem power management driver");
1157 MODULE_LICENSE("GPL");