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