misc: tegra-baseband: short autosuspend for native remote wake
[linux-2.6.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/usb.h>
27 #include <linux/err.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/suspend.h>
30 #include <linux/slab.h>
31 #include <linux/wakelock.h>
32 #include <linux/pm_qos.h>
33 #include <linux/edp.h>
34 #include <mach/gpio-tegra.h>
35 #include <linux/platform_data/tegra_usb_modem_power.h>
36
37 #define BOOST_CPU_FREQ_MIN      1200000
38 #define BOOST_CPU_FREQ_TIMEOUT  5000
39
40 #define WAKELOCK_TIMEOUT_FOR_USB_ENUM           (HZ * 10)
41 #define WAKELOCK_TIMEOUT_FOR_REMOTE_WAKE        (HZ)
42
43 #define MAX_MODEM_EDP_STATES 10
44
45 struct tegra_usb_modem {
46         struct tegra_usb_modem_power_platform_data *pdata;
47         struct platform_device *pdev;
48         unsigned int wake_cnt;  /* remote wakeup counter */
49         unsigned int wake_irq;  /* remote wakeup irq */
50         unsigned int boot_irq;  /* modem boot irq */
51         struct mutex lock;
52         struct wake_lock wake_lock;     /* modem wake lock */
53         unsigned int vid;       /* modem vendor id */
54         unsigned int pid;       /* modem product id */
55         struct usb_device *udev;        /* modem usb device */
56         struct usb_device *parent;      /* parent device */
57         struct usb_interface *intf;     /* first modem usb interface */
58         struct workqueue_struct *wq;    /* modem workqueue */
59         struct delayed_work recovery_work;      /* modem recovery work */
60         struct work_struct host_load_work;      /* usb host load work */
61         struct work_struct host_unload_work;    /* usb host unload work */
62         struct pm_qos_request cpu_boost_req; /* min CPU freq request */
63         struct work_struct cpu_boost_work;      /* CPU freq boost work */
64         struct delayed_work cpu_unboost_work;   /* CPU freq unboost work */
65         const struct tegra_modem_operations *ops;       /* modem operations */
66         unsigned int capability;        /* modem capability */
67         int system_suspend;     /* system suspend flag */
68         struct notifier_block pm_notifier;      /* pm event notifier */
69         struct notifier_block usb_notifier;     /* usb event notifier */
70         int sysfs_file_created;
71         int short_autosuspend_enabled;
72         struct platform_device *hc;     /* USB host controller */
73         struct mutex hc_lock;
74         struct edp_client *modem_boot_edp_client;
75         struct edp_client modem_edp_client;
76         unsigned int modem_edp_states[MAX_MODEM_EDP_STATES];
77         int edp_client_registered;
78         int edp_boot_client_registered;
79         int edp_initialized;
80         char *edp_manager_name;
81         unsigned int i_breach_ppm; /* percent time current exceeds i_thresh */
82         unsigned int i_thresh_3g_adjperiod; /* 3g i_thresh adj period */
83         unsigned int i_thresh_lte_adjperiod; /* lte i_thresh adj period */
84         struct work_struct edp_work;
85         struct mutex edp_lock;
86 };
87
88
89 /* supported modems */
90 static const struct usb_device_id modem_list[] = {
91         {USB_DEVICE(0x1983, 0x0310),    /* Icera 450 rev1 */
92          .driver_info = TEGRA_MODEM_AUTOSUSPEND,
93          },
94         {USB_DEVICE(0x1983, 0x0321),    /* Icera 450 rev2 */
95          .driver_info = TEGRA_MODEM_AUTOSUSPEND,
96          },
97         {USB_DEVICE(0x1983, 0x0327),    /* Icera 450 5AE */
98          .driver_info = TEGRA_MODEM_AUTOSUSPEND,
99          },
100         {USB_DEVICE(0x1983, 0x0427),    /* Icera 500 5AN */
101          .driver_info = TEGRA_MODEM_AUTOSUSPEND,
102          },
103         {USB_DEVICE(0x1983, 0x1005),    /* Icera 500 5AN (BSD) */
104         /* .driver_info = TEGRA_MODEM_AUTOSUSPEND, */
105          },
106         {USB_DEVICE(0x1983, 0x1007),    /* Icera 500 Nemo */
107          .driver_info = TEGRA_USB_HOST_RELOAD,
108          },
109         {}
110 };
111
112 static ssize_t load_unload_usb_host(struct device *dev,
113                                     struct device_attribute *attr,
114                                     const char *buf, size_t count);
115
116 static void cpu_freq_unboost(struct work_struct *ws)
117 {
118         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
119                                                      cpu_unboost_work.work);
120
121         pm_qos_update_request(&modem->cpu_boost_req, PM_QOS_DEFAULT_VALUE);
122 }
123
124 static void cpu_freq_boost(struct work_struct *ws)
125 {
126         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
127                                                      cpu_boost_work);
128
129         cancel_delayed_work_sync(&modem->cpu_unboost_work);
130         pm_qos_update_request(&modem->cpu_boost_req, BOOST_CPU_FREQ_MIN);
131         queue_delayed_work(modem->wq, &modem->cpu_unboost_work,
132                               msecs_to_jiffies(BOOST_CPU_FREQ_TIMEOUT));
133 }
134
135 static void edp_work(struct work_struct *ws)
136 {
137         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
138                                                      edp_work);
139         struct edp_manager *mgr;
140         int ret;
141
142         mutex_lock(&modem->edp_lock);
143         if (!modem->edp_client_registered &&
144             modem->edp_boot_client_registered) {
145                 pr_err("modem boot client already registered\n");
146                 goto done;
147         }
148
149         /* unregister modem client */
150         ret = edp_unregister_client(&modem->modem_edp_client);
151         if (ret) {
152                 pr_err("edp unregistration failed\n");
153                 goto done;
154         }
155
156         modem->edp_client_registered = 0;
157
158         /* register modem boot client */
159         mgr = edp_get_manager(modem->edp_manager_name);
160         if (!mgr) {
161                 pr_err("can't get edp manager\n");
162                 goto done;
163         }
164
165         ret = edp_register_client(mgr, modem->modem_boot_edp_client);
166         if (ret) {
167                 pr_err("unable to register modem boot edp client\n");
168                 goto done;
169         }
170
171         /* request E0 */
172         ret = edp_update_client_request(modem->modem_boot_edp_client,
173                                         0, NULL);
174         if (ret) {
175                 pr_err("unable to set E0 state\n");
176                 goto done;
177         }
178
179         modem->edp_boot_client_registered = 1;
180
181 done:
182         mutex_unlock(&modem->edp_lock);
183 }
184
185 static irqreturn_t tegra_usb_modem_wake_thread(int irq, void *data)
186 {
187         struct tegra_usb_modem *modem = (struct tegra_usb_modem *)data;
188
189         mutex_lock(&modem->lock);
190         if (modem->udev && modem->udev->state != USB_STATE_NOTATTACHED) {
191                 dev_info(&modem->pdev->dev, "remote wake (%u)\n",
192                          ++(modem->wake_cnt));
193
194                 if (!modem->system_suspend) {
195                         wake_lock_timeout(&modem->wake_lock,
196                                           WAKELOCK_TIMEOUT_FOR_REMOTE_WAKE);
197
198                         usb_lock_device(modem->udev);
199                         if (usb_autopm_get_interface(modem->intf) == 0)
200                                 usb_autopm_put_interface_async(modem->intf);
201                         usb_unlock_device(modem->udev);
202                 }
203 #ifdef CONFIG_PM
204                 if (modem->capability & TEGRA_MODEM_AUTOSUSPEND &&
205                     modem->short_autosuspend_enabled) {
206                         pm_runtime_set_autosuspend_delay(&modem->udev->dev,
207                                         modem->pdata->autosuspend_delay);
208                         modem->short_autosuspend_enabled = 0;
209                 }
210 #endif
211         }
212         mutex_unlock(&modem->lock);
213
214         return IRQ_HANDLED;
215 }
216
217 static irqreturn_t tegra_usb_modem_boot_thread(int irq, void *data)
218 {
219         struct tegra_usb_modem *modem = (struct tegra_usb_modem *)data;
220         int v = gpio_get_value(modem->pdata->boot_gpio);
221
222         dev_info(&modem->pdev->dev, "MDM_COLDBOOT %s\n", v ? "high" : "low");
223         if (modem->capability & TEGRA_USB_HOST_RELOAD)
224                 if (!work_pending(&modem->host_load_work) &&
225                     !work_pending(&modem->host_unload_work))
226                         queue_work(modem->wq, v ? &modem->host_load_work :
227                                    &modem->host_unload_work);
228
229         /* hold wait lock to complete the enumeration */
230         wake_lock_timeout(&modem->wake_lock, WAKELOCK_TIMEOUT_FOR_USB_ENUM);
231
232         /* boost CPU freq */
233         if (!work_pending(&modem->cpu_boost_work))
234                 queue_work(modem->wq, &modem->cpu_boost_work);
235
236         if (modem->edp_initialized && !v)
237                 queue_work(modem->wq, &modem->edp_work);
238
239         /* USB disconnect maybe on going... */
240         mutex_lock(&modem->lock);
241         if (modem->udev && modem->udev->state != USB_STATE_NOTATTACHED)
242                 pr_warn("Device is not disconnected!\n");
243         mutex_unlock(&modem->lock);
244
245         return IRQ_HANDLED;
246 }
247
248 static void tegra_usb_modem_recovery(struct work_struct *ws)
249 {
250         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
251                                                      recovery_work.work);
252
253         mutex_lock(&modem->lock);
254         if (!modem->udev) {     /* assume modem crashed */
255                 if (modem->ops && modem->ops->reset)
256                         modem->ops->reset();
257         }
258         mutex_unlock(&modem->lock);
259 }
260
261 static void tegra_usb_host_load(struct work_struct *ws)
262 {
263         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
264                                                      host_load_work);
265         load_unload_usb_host(&modem->pdev->dev, NULL, "1", 1);
266 }
267
268 static void tegra_usb_host_unload(struct work_struct *ws)
269 {
270         struct tegra_usb_modem *modem = container_of(ws, struct tegra_usb_modem,
271                                                      host_unload_work);
272         load_unload_usb_host(&modem->pdev->dev, NULL, "0", 1);
273 }
274
275 static void device_add_handler(struct tegra_usb_modem *modem,
276                                struct usb_device *udev)
277 {
278         const struct usb_device_descriptor *desc = &udev->descriptor;
279         struct usb_interface *intf = usb_ifnum_to_if(udev, 0);
280         const struct usb_device_id *id = NULL;
281
282         if (intf) {
283                 /* only look for specific modems if modem_list is provided in
284                    platform data. Otherwise, look for the modems in the default
285                    supported modem list */
286                 if (modem->pdata->modem_list)
287                         id = usb_match_id(intf, modem->pdata->modem_list);
288                 else
289                         id = usb_match_id(intf, modem_list);
290         }
291
292         if (id) {
293                 /* hold wakelock to ensure ril has enough time to restart */
294                 wake_lock_timeout(&modem->wake_lock,
295                                   WAKELOCK_TIMEOUT_FOR_USB_ENUM);
296
297                 pr_info("Add device %d <%s %s>\n", udev->devnum,
298                         udev->manufacturer, udev->product);
299
300                 mutex_lock(&modem->lock);
301                 modem->udev = udev;
302                 modem->parent = udev->parent;
303                 modem->intf = intf;
304                 modem->vid = desc->idVendor;
305                 modem->pid = desc->idProduct;
306                 modem->wake_cnt = 0;
307                 modem->capability = id->driver_info;
308                 mutex_unlock(&modem->lock);
309
310                 pr_info("persist_enabled: %u\n", udev->persist_enabled);
311
312 #ifdef CONFIG_PM
313                 if (modem->capability & TEGRA_MODEM_AUTOSUSPEND) {
314                         pm_runtime_set_autosuspend_delay(&udev->dev,
315                                         modem->pdata->autosuspend_delay);
316                         modem->short_autosuspend_enabled = 0;
317                         usb_enable_autosuspend(udev);
318                         pr_info("enable autosuspend for %s %s\n",
319                                 udev->manufacturer, udev->product);
320                 }
321
322                 /* allow the device to wake up the system */
323                 if (udev->actconfig->desc.bmAttributes &
324                     USB_CONFIG_ATT_WAKEUP)
325                         device_set_wakeup_enable(&udev->dev, true);
326
327 #endif
328         }
329 }
330
331 static void device_remove_handler(struct tegra_usb_modem *modem,
332                                   struct usb_device *udev)
333 {
334         const struct usb_device_descriptor *desc = &udev->descriptor;
335
336         if (desc->idVendor == modem->vid && desc->idProduct == modem->pid) {
337                 pr_info("Remove device %d <%s %s>\n", udev->devnum,
338                         udev->manufacturer, udev->product);
339
340                 mutex_lock(&modem->lock);
341                 modem->udev = NULL;
342                 modem->intf = NULL;
343                 modem->vid = 0;
344                 mutex_unlock(&modem->lock);
345
346                 if (modem->capability & TEGRA_MODEM_RECOVERY)
347                         queue_delayed_work(modem->wq,
348                                            &modem->recovery_work, HZ * 10);
349         }
350 }
351
352 static int mdm_usb_notifier(struct notifier_block *notifier,
353                             unsigned long usb_event, void *udev)
354 {
355         struct tegra_usb_modem *modem =
356             container_of(notifier, struct tegra_usb_modem, usb_notifier);
357
358         switch (usb_event) {
359         case USB_DEVICE_ADD:
360                 device_add_handler(modem, udev);
361                 break;
362         case USB_DEVICE_REMOVE:
363                 device_remove_handler(modem, udev);
364                 break;
365         }
366         return NOTIFY_OK;
367 }
368
369 static int mdm_pm_notifier(struct notifier_block *notifier,
370                            unsigned long pm_event, void *unused)
371 {
372         struct tegra_usb_modem *modem =
373             container_of(notifier, struct tegra_usb_modem, pm_notifier);
374
375         mutex_lock(&modem->lock);
376         if (!modem->udev) {
377                 mutex_unlock(&modem->lock);
378                 return NOTIFY_DONE;
379         }
380
381         pr_info("%s: event %ld\n", __func__, pm_event);
382         switch (pm_event) {
383         case PM_SUSPEND_PREPARE:
384                 if (wake_lock_active(&modem->wake_lock)) {
385                         pr_warn("%s: wakelock was active, aborting suspend\n",
386                                 __func__);
387                         mutex_unlock(&modem->lock);
388                         return NOTIFY_STOP;
389                 }
390
391                 modem->system_suspend = 1;
392 #ifdef CONFIG_PM
393                 if (modem->capability & TEGRA_MODEM_AUTOSUSPEND &&
394                     modem->udev &&
395                     modem->udev->state != USB_STATE_NOTATTACHED) {
396                         pm_runtime_set_autosuspend_delay(&modem->udev->dev,
397                                         modem->pdata->short_autosuspend_delay);
398                         modem->short_autosuspend_enabled = 1;
399                 }
400 #endif
401                 mutex_unlock(&modem->lock);
402                 return NOTIFY_OK;
403         case PM_POST_SUSPEND:
404                 modem->system_suspend = 0;
405                 mutex_unlock(&modem->lock);
406                 return NOTIFY_OK;
407         }
408
409         mutex_unlock(&modem->lock);
410         return NOTIFY_DONE;
411 }
412
413 static int mdm_request_wakeable_irq(struct tegra_usb_modem *modem,
414                                     irq_handler_t thread_fn,
415                                     unsigned int irq_gpio,
416                                     unsigned long irq_flags,
417                                     const char *label, unsigned int *irq)
418 {
419         int ret;
420
421         ret = gpio_request(irq_gpio, label);
422         if (ret)
423                 return ret;
424
425         /* enable IRQ for GPIO */
426         *irq = gpio_to_irq(irq_gpio);
427
428         /* request threaded irq for GPIO */
429         ret = request_threaded_irq(*irq, NULL, thread_fn, irq_flags, label,
430                                    modem);
431         if (ret)
432                 return ret;
433
434         ret = enable_irq_wake(*irq);
435         if (ret) {
436                 free_irq(*irq, modem);
437                 return ret;
438         }
439
440         return ret;
441 }
442
443 static void tegra_usb_modem_post_remote_wakeup(void)
444 {
445         struct device *dev;
446         struct tegra_usb_modem *modem;
447
448         dev = bus_find_device_by_name(&platform_bus_type, NULL,
449                                         "MDM");
450         if (!dev) {
451                 pr_warn("%s unable to find device name\n", __func__);
452                 return;
453         }
454
455         modem = dev_get_drvdata(dev);
456
457         mutex_lock(&modem->lock);
458 #ifdef CONFIG_PM
459         if (modem->capability & TEGRA_MODEM_AUTOSUSPEND &&
460             modem->udev &&
461             modem->udev->state != USB_STATE_NOTATTACHED &&
462             modem->short_autosuspend_enabled) {
463                 pm_runtime_set_autosuspend_delay(&modem->udev->dev,
464                                 modem->pdata->autosuspend_delay);
465                 modem->short_autosuspend_enabled = 0;
466         }
467 #endif
468         wake_lock_timeout(&modem->wake_lock, WAKELOCK_TIMEOUT_FOR_REMOTE_WAKE);
469         mutex_unlock(&modem->lock);
470
471         return;
472 }
473
474 /* load USB host controller */
475 static struct platform_device *tegra_usb_host_register(
476                                 const struct tegra_usb_modem *modem)
477 {
478         const struct platform_device *hc_device =
479             modem->pdata->tegra_ehci_device;
480         struct platform_device *pdev;
481         int val;
482
483         pdev = platform_device_alloc(hc_device->name, hc_device->id);
484         if (!pdev)
485                 return NULL;
486
487         val = platform_device_add_resources(pdev, hc_device->resource,
488                                             hc_device->num_resources);
489         if (val)
490                 goto error;
491
492         pdev->dev.dma_mask = hc_device->dev.dma_mask;
493         pdev->dev.coherent_dma_mask = hc_device->dev.coherent_dma_mask;
494
495         val = platform_device_add_data(pdev, modem->pdata->tegra_ehci_pdata,
496                                        sizeof(struct tegra_usb_platform_data));
497         if (val)
498                 goto error;
499
500         val = platform_device_add(pdev);
501         if (val)
502                 goto error;
503
504         return pdev;
505
506 error:
507         pr_err("%s: err %d\n", __func__, val);
508         platform_device_put(pdev);
509         return NULL;
510 }
511
512 /* unload USB host controller */
513 static void tegra_usb_host_unregister(struct platform_device *pdev)
514 {
515         platform_device_unregister(pdev);
516 }
517
518 static ssize_t show_usb_host(struct device *dev,
519                              struct device_attribute *attr, char *buf)
520 {
521         struct tegra_usb_modem *modem = dev_get_drvdata(dev);
522
523         return sprintf(buf, "%d\n", (modem->hc) ? 1 : 0);
524 }
525
526 static ssize_t load_unload_usb_host(struct device *dev,
527                                     struct device_attribute *attr,
528                                     const char *buf, size_t count)
529 {
530         struct tegra_usb_modem *modem = dev_get_drvdata(dev);
531         int host;
532
533         if (sscanf(buf, "%d", &host) != 1 || host < 0 || host > 1)
534                 return -EINVAL;
535
536         pr_info("%s USB host\n", (host) ? "load" : "unload");
537
538         mutex_lock(&modem->hc_lock);
539         if (host) {
540                 if (!modem->hc)
541                         modem->hc = tegra_usb_host_register(modem);
542         } else {
543                 if (modem->hc) {
544                         tegra_usb_host_unregister(modem->hc);
545                         modem->hc = NULL;
546                 }
547         }
548         mutex_unlock(&modem->hc_lock);
549
550         return count;
551 }
552
553 static DEVICE_ATTR(load_host, S_IRUSR | S_IWUSR, show_usb_host,
554                    load_unload_usb_host);
555
556 #define EDP_INT_ATTR(field)                                             \
557 static ssize_t                                                          \
558 field ## _show(struct device *pdev, struct device_attribute *attr,      \
559                char *buf)                                               \
560 {                                                                       \
561         struct tegra_usb_modem *modem = dev_get_drvdata(pdev);          \
562                                                                         \
563         if (!modem)                                                     \
564                 return -EAGAIN;                                         \
565                                                                         \
566         return sprintf(buf, "%d\n", modem->field);                      \
567 }                                                                       \
568 static DEVICE_ATTR(field, S_IRUSR, field ## _show, NULL);
569
570 EDP_INT_ATTR(i_breach_ppm);
571 EDP_INT_ATTR(i_thresh_3g_adjperiod);
572 EDP_INT_ATTR(i_thresh_lte_adjperiod);
573
574 static ssize_t i_max_show(struct device *pdev, struct device_attribute *attr,
575                           char *buf)
576 {
577         struct tegra_usb_modem *modem = dev_get_drvdata(pdev);
578         int i = 0;
579         int count = 0;
580
581         mutex_lock(&modem->edp_lock);
582         for (i = 0; i < MAX_MODEM_EDP_STATES; i++) {
583                 if (modem->modem_edp_states[i] <= 0)
584                         break;
585
586                 count += sprintf(&buf[count], "%d ",
587                                  modem->modem_edp_states[i]);
588         }
589         mutex_unlock(&modem->edp_lock);
590
591         count += sprintf(&buf[count], "\n");
592
593         return count;
594 }
595
596 static ssize_t i_max_store(struct device *pdev, struct device_attribute *attr,
597                            const char *buff, size_t size)
598 {
599         struct tegra_usb_modem *modem;
600         char *s, *state_i_max, buf[50];
601         unsigned int num_states = 0;
602         struct edp_manager *mgr;
603         int ret;
604
605         modem = (struct tegra_usb_modem *)dev_get_drvdata(pdev);
606
607         mutex_lock(&modem->edp_lock);
608
609         /* client should only be registered once per modem boot */
610         if (modem->edp_client_registered) {
611                 pr_err("modem edp client already registered\n");
612                 ret = -EBUSY;
613                 goto done;
614         }
615
616         memset(modem->modem_edp_states, 0, sizeof(modem->modem_edp_states));
617         memset(&modem->modem_edp_client, 0, sizeof(modem->modem_edp_client));
618
619         /* retrieve max current for supported states */
620         strlcpy(buf, buff, sizeof(buf));
621         s = strim(buf);
622         while (s && (num_states < MAX_MODEM_EDP_STATES)) {
623                 state_i_max = strsep(&s, ",");
624                 ret = kstrtoul(state_i_max, 10,
625                         (unsigned long *)&modem->modem_edp_states[num_states]);
626                 if (ret) {
627                         pr_err("invalid modem state-current setting\n");
628                         goto done;
629                 }
630                 num_states++;
631         }
632
633         if (s && (num_states == MAX_MODEM_EDP_STATES)) {
634                 pr_err("number of modem EDP states exceeded max\n");
635                 ret = -EINVAL;
636                 goto done;
637         }
638
639         strncpy(modem->modem_edp_client.name, "modem", EDP_NAME_LEN);
640         modem->modem_edp_client.name[EDP_NAME_LEN - 1] = '\0';
641         modem->modem_edp_client.states = modem->modem_edp_states;
642         modem->modem_edp_client.num_states = num_states;
643         modem->modem_edp_client.e0_index = 0;
644         modem->modem_edp_client.priority = EDP_MAX_PRIO;
645
646         mgr = edp_get_manager(modem->edp_manager_name);
647         if (!mgr) {
648                 dev_err(pdev, "can't get edp manager\n");
649                 ret = -EINVAL;
650                 goto done;
651         }
652
653         /* register modem client */
654         ret = edp_register_client(mgr, &modem->modem_edp_client);
655         if (ret) {
656                 dev_err(pdev, "unable to register modem edp client\n");
657                 goto done;
658         }
659         modem->edp_client_registered = 1;
660
661         /* unregister modem_boot_client */
662         ret = edp_unregister_client(modem->modem_boot_edp_client);
663         if (ret) {
664                 dev_err(pdev, "unable to register modem boot edp client\n");
665                 goto done;
666         }
667
668         modem->edp_boot_client_registered = 0;
669         ret = size;
670
671 done:
672         mutex_unlock(&modem->edp_lock);
673         return ret;
674 }
675 static DEVICE_ATTR(i_max, S_IRUSR | S_IWUSR, i_max_show, i_max_store);
676
677 static ssize_t request_store(struct device *pdev, struct device_attribute *attr,
678                              const char *buff, size_t size)
679 {
680         struct tegra_usb_modem *modem = dev_get_drvdata(pdev);
681         struct edp_client *c;
682         unsigned int id;
683         int ret;
684
685         if (sscanf(buff, "%u", &id) != 1)
686                 return -EINVAL;
687
688         if (!modem->edp_client_registered)
689                 return -EINVAL;
690
691         c = &modem->modem_edp_client;
692         if (id >= c->num_states)
693                 return -EINVAL;
694
695         ret = edp_update_client_request(c, id, NULL);
696         if (ret)
697                 dev_err(pdev, "state update to %u failed\n", id);
698         else
699                 ret = size;
700
701         return ret;
702 }
703 static DEVICE_ATTR(request, S_IWUSR, NULL, request_store);
704
705 static ssize_t threshold_store(struct device *pdev,
706                                struct device_attribute *attr,
707                                const char *buff, size_t size)
708 {
709         struct tegra_usb_modem *modem = dev_get_drvdata(pdev);
710         unsigned int tv;
711         int ret;
712
713         if (sscanf(buff, "%u", &tv) != 1)
714                 return -EINVAL;
715
716         if (!modem->edp_client_registered)
717                 return -EINVAL;
718
719         ret = edp_update_loan_threshold(&modem->modem_edp_client, tv);
720         if (ret)
721                 dev_err(pdev, "threshold update to %u failed\n", tv);
722         else
723                 ret = size;
724
725         return ret;
726 }
727 DEVICE_ATTR(threshold, S_IWUSR, NULL, threshold_store);
728
729 static struct device_attribute *edp_attributes[] = {
730         &dev_attr_i_breach_ppm,
731         &dev_attr_i_thresh_3g_adjperiod,
732         &dev_attr_i_thresh_lte_adjperiod,
733         &dev_attr_i_max,
734         &dev_attr_request,
735         &dev_attr_threshold,
736         NULL
737 };
738
739 static struct tegra_usb_phy_platform_ops tegra_usb_modem_platform_ops = {
740         .post_remote_wakeup = tegra_usb_modem_post_remote_wakeup,
741 };
742
743 static int mdm_init(struct tegra_usb_modem *modem, struct platform_device *pdev)
744 {
745         struct tegra_usb_modem_power_platform_data *pdata =
746             pdev->dev.platform_data;
747         int ret = 0;
748         struct edp_manager *mgr;
749         struct device_attribute **edp_attrs;
750         struct device_attribute *attr;
751
752         modem->pdata = pdata;
753         modem->pdev = pdev;
754
755         if (pdata->modem_boot_edp_client && pdata->edp_manager_name) {
756                 mutex_init(&modem->edp_lock);
757
758                 /* register modem boot client */
759                 modem->edp_manager_name = pdata->edp_manager_name;
760                 mgr = edp_get_manager(pdata->edp_manager_name);
761                 if (!mgr) {
762                         dev_err(&pdev->dev, "can't get edp manager\n");
763                         goto error;
764                 }
765
766                 modem->modem_boot_edp_client = pdata->modem_boot_edp_client;
767                 ret = edp_register_client(mgr, modem->modem_boot_edp_client);
768                 if (ret) {
769                         dev_err(&pdev->dev,
770                                 "unable to register modem boot edp client\n");
771                         goto error;
772                 }
773
774                 /* request E0 */
775                 ret = edp_update_client_request(modem->modem_boot_edp_client,
776                                                 0, NULL);
777                 if (ret) {
778                         dev_err(&pdev->dev,
779                                 "unable to set e0 state\n");
780                         goto error;
781                 }
782
783                 modem->edp_boot_client_registered = 1;
784
785                 modem->i_breach_ppm = pdata->i_breach_ppm;
786                 modem->i_thresh_3g_adjperiod = pdata->i_thresh_3g_adjperiod;
787                 modem->i_thresh_lte_adjperiod = pdata->i_thresh_lte_adjperiod;
788
789                 edp_attrs = edp_attributes;
790                 while ((attr = *edp_attrs++)) {
791                         ret = device_create_file(&pdev->dev, attr);
792                         if (ret) {
793                                 dev_err(&pdev->dev, "can't create sysfs file\n");
794                                 goto error;
795                         }
796                 }
797
798                 INIT_WORK(&modem->edp_work, edp_work);
799
800                 modem->edp_initialized = 1;
801         }
802
803         /* get modem operations from platform data */
804         modem->ops = (const struct tegra_modem_operations *)pdata->ops;
805
806         if (modem->ops) {
807                 /* modem init */
808                 if (modem->ops->init) {
809                         ret = modem->ops->init();
810                         if (ret)
811                                 return ret;
812                 }
813
814                 /* start modem */
815                 if (modem->ops->start)
816                         modem->ops->start();
817         }
818
819         /* create sysfs node to load/unload host controller */
820         ret = device_create_file(&pdev->dev, &dev_attr_load_host);
821         if (ret) {
822                 dev_err(&pdev->dev, "can't create sysfs file\n");
823                 goto error;
824         }
825         modem->sysfs_file_created = 1;
826         modem->capability = TEGRA_USB_HOST_RELOAD;
827         mutex_init(&(modem->lock));
828         mutex_init(&modem->hc_lock);
829         wake_lock_init(&modem->wake_lock, WAKE_LOCK_SUSPEND, "mdm_lock");
830         if (pdev->id >= 0)
831                 dev_set_name(&pdev->dev, "MDM%d", pdev->id);
832         else
833                 dev_set_name(&pdev->dev, "MDM");
834
835         /* create work queue platform_driver_registe */
836         modem->wq = create_workqueue("tegra_usb_mdm_queue");
837         INIT_DELAYED_WORK(&modem->recovery_work, tegra_usb_modem_recovery);
838         INIT_WORK(&modem->host_load_work, tegra_usb_host_load);
839         INIT_WORK(&modem->host_unload_work, tegra_usb_host_unload);
840         INIT_WORK(&modem->cpu_boost_work, cpu_freq_boost);
841         INIT_DELAYED_WORK(&modem->cpu_unboost_work, cpu_freq_unboost);
842
843         pm_qos_add_request(&modem->cpu_boost_req, PM_QOS_CPU_FREQ_MIN,
844                            PM_QOS_DEFAULT_VALUE);
845
846         /* if wake gpio is not specified we rely on native usb remote wake */
847         if (gpio_is_valid(pdata->wake_gpio)) {
848                 /* request remote wakeup irq from platform data */
849                 ret = mdm_request_wakeable_irq(modem,
850                                                tegra_usb_modem_wake_thread,
851                                                pdata->wake_gpio,
852                                                pdata->wake_irq_flags,
853                                                "mdm_wake", &modem->wake_irq);
854                 if (ret) {
855                         dev_err(&pdev->dev, "request wake irq error\n");
856                         goto error;
857                 }
858         } else {
859                 modem->pdata->tegra_ehci_pdata->ops =
860                                                 &tegra_usb_modem_platform_ops;
861         }
862
863
864         if (gpio_is_valid(pdata->boot_gpio)) {
865                 /* request boot irq from platform data */
866                 ret = mdm_request_wakeable_irq(modem,
867                                                tegra_usb_modem_boot_thread,
868                                                pdata->boot_gpio,
869                                                pdata->boot_irq_flags,
870                                                "mdm_boot", &modem->boot_irq);
871                 if (ret) {
872                         dev_err(&pdev->dev, "request boot irq error\n");
873                         goto error;
874                 }
875         } else {
876                 dev_err(&pdev->dev, "boot irq not specified\n");
877                 goto error;
878         }
879
880         modem->pm_notifier.notifier_call = mdm_pm_notifier;
881         modem->usb_notifier.notifier_call = mdm_usb_notifier;
882
883         usb_register_notify(&modem->usb_notifier);
884         register_pm_notifier(&modem->pm_notifier);
885
886         return ret;
887 error:
888         if (modem->sysfs_file_created)
889                 device_remove_file(&pdev->dev, &dev_attr_load_host);
890
891         if (modem->wake_irq) {
892                 disable_irq_wake(modem->wake_irq);
893                 free_irq(modem->wake_irq, modem);
894         }
895
896         if (modem->boot_irq) {
897                 disable_irq_wake(modem->boot_irq);
898                 free_irq(modem->boot_irq, modem);
899         }
900
901         if (modem->edp_initialized) {
902                 edp_attrs = edp_attributes;
903                 while ((attr = *edp_attrs++))
904                         device_remove_file(&pdev->dev, attr);
905         }
906
907         if (modem->edp_boot_client_registered)
908                 edp_unregister_client(modem->modem_boot_edp_client);
909
910         return ret;
911 }
912
913 static int tegra_usb_modem_probe(struct platform_device *pdev)
914 {
915         struct tegra_usb_modem_power_platform_data *pdata =
916             pdev->dev.platform_data;
917         struct tegra_usb_modem *modem;
918         int ret = 0;
919
920         if (!pdata) {
921                 dev_dbg(&pdev->dev, "platform_data not available\n");
922                 return -EINVAL;
923         }
924
925         modem = kzalloc(sizeof(struct tegra_usb_modem), GFP_KERNEL);
926         if (!modem) {
927                 dev_dbg(&pdev->dev, "failed to allocate memory\n");
928                 return -ENOMEM;
929         }
930
931         ret = mdm_init(modem, pdev);
932         if (ret) {
933                 kfree(modem);
934                 return ret;
935         }
936
937         dev_set_drvdata(&pdev->dev, modem);
938
939         return ret;
940 }
941
942 static int __exit tegra_usb_modem_remove(struct platform_device *pdev)
943 {
944         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
945         struct device_attribute **edp_attrs;
946         struct device_attribute *attr;
947
948         unregister_pm_notifier(&modem->pm_notifier);
949         usb_unregister_notify(&modem->usb_notifier);
950
951         if (modem->wake_irq) {
952                 disable_irq_wake(modem->wake_irq);
953                 free_irq(modem->wake_irq, modem);
954         }
955
956         if (modem->boot_irq) {
957                 disable_irq_wake(modem->boot_irq);
958                 free_irq(modem->boot_irq, modem);
959         }
960
961         if (modem->sysfs_file_created)
962                 device_remove_file(&pdev->dev, &dev_attr_load_host);
963
964         if (modem->edp_initialized) {
965                 cancel_work_sync(&modem->edp_work);
966
967                 edp_attrs = edp_attributes;
968                 while ((attr = *edp_attrs++))
969                         device_remove_file(&pdev->dev, attr);
970         }
971
972         if (modem->edp_boot_client_registered)
973                 edp_unregister_client(modem->modem_boot_edp_client);
974
975         if (modem->edp_client_registered)
976                 edp_unregister_client(&modem->modem_edp_client);
977
978         cancel_delayed_work_sync(&modem->recovery_work);
979         cancel_work_sync(&modem->host_load_work);
980         cancel_work_sync(&modem->host_unload_work);
981         cancel_work_sync(&modem->cpu_boost_work);
982         cancel_delayed_work_sync(&modem->cpu_unboost_work);
983         destroy_workqueue(modem->wq);
984
985         pm_qos_remove_request(&modem->cpu_boost_req);
986
987         kfree(modem);
988         return 0;
989 }
990
991 static void tegra_usb_modem_shutdown(struct platform_device *pdev)
992 {
993         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
994
995         if (modem->ops && modem->ops->stop)
996                 modem->ops->stop();
997 }
998
999 #ifdef CONFIG_PM
1000 static int tegra_usb_modem_suspend(struct platform_device *pdev,
1001                                    pm_message_t state)
1002 {
1003         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1004
1005         /* send L3 hint to modem */
1006         if (modem->ops && modem->ops->suspend)
1007                 modem->ops->suspend();
1008         return 0;
1009 }
1010
1011 static int tegra_usb_modem_resume(struct platform_device *pdev)
1012 {
1013         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1014
1015         /* send L3->L0 hint to modem */
1016         if (modem->ops && modem->ops->resume)
1017                 modem->ops->resume();
1018         return 0;
1019 }
1020 #endif
1021
1022 static struct platform_driver tegra_usb_modem_power_driver = {
1023         .driver = {
1024                    .name = "tegra_usb_modem_power",
1025                    .owner = THIS_MODULE,
1026                    },
1027         .probe = tegra_usb_modem_probe,
1028         .remove = __exit_p(tegra_usb_modem_remove),
1029         .shutdown = tegra_usb_modem_shutdown,
1030 #ifdef CONFIG_PM
1031         .suspend = tegra_usb_modem_suspend,
1032         .resume = tegra_usb_modem_resume,
1033 #endif
1034 };
1035
1036 static int __init tegra_usb_modem_power_init(void)
1037 {
1038         return platform_driver_register(&tegra_usb_modem_power_driver);
1039 }
1040
1041 module_init(tegra_usb_modem_power_init);
1042
1043 static void __exit tegra_usb_modem_power_exit(void)
1044 {
1045         platform_driver_unregister(&tegra_usb_modem_power_driver);
1046 }
1047
1048 module_exit(tegra_usb_modem_power_exit);
1049
1050 MODULE_DESCRIPTION("Tegra usb modem power management driver");
1051 MODULE_LICENSE("GPL");