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