28f5202aa082b68b3efd5570919fda7b16f25b6e
[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->wake_irq &&
395                     modem->udev &&
396                     modem->udev->state != USB_STATE_NOTATTACHED) {
397                         pm_runtime_set_autosuspend_delay(&modem->udev->dev,
398                                         modem->pdata->short_autosuspend_delay);
399                         modem->short_autosuspend_enabled = 1;
400                 }
401 #endif
402                 mutex_unlock(&modem->lock);
403                 return NOTIFY_OK;
404         case PM_POST_SUSPEND:
405                 modem->system_suspend = 0;
406                 mutex_unlock(&modem->lock);
407                 return NOTIFY_OK;
408         }
409
410         mutex_unlock(&modem->lock);
411         return NOTIFY_DONE;
412 }
413
414 static int mdm_request_wakeable_irq(struct tegra_usb_modem *modem,
415                                     irq_handler_t thread_fn,
416                                     unsigned int irq_gpio,
417                                     unsigned long irq_flags,
418                                     const char *label, unsigned int *irq)
419 {
420         int ret;
421
422         ret = gpio_request(irq_gpio, label);
423         if (ret)
424                 return ret;
425
426         /* enable IRQ for GPIO */
427         *irq = gpio_to_irq(irq_gpio);
428
429         /* request threaded irq for GPIO */
430         ret = request_threaded_irq(*irq, NULL, thread_fn, irq_flags, label,
431                                    modem);
432         if (ret)
433                 return ret;
434
435         ret = enable_irq_wake(*irq);
436         if (ret) {
437                 free_irq(*irq, modem);
438                 return ret;
439         }
440
441         return ret;
442 }
443
444 static void tegra_usb_modem_post_remote_wakeup(void)
445 {
446         struct device *dev;
447         struct tegra_usb_modem *modem;
448
449         dev = bus_find_device_by_name(&platform_bus_type, NULL,
450                                         "MDM");
451         if (!dev) {
452                 pr_warn("%s unable to find device name\n", __func__);
453                 return;
454         }
455
456         modem = dev_get_drvdata(dev);
457
458         mutex_lock(&modem->lock);
459         wake_lock_timeout(&modem->wake_lock, WAKELOCK_TIMEOUT_FOR_REMOTE_WAKE);
460         mutex_unlock(&modem->lock);
461
462         return;
463 }
464
465 /* load USB host controller */
466 static struct platform_device *tegra_usb_host_register(
467                                 const struct tegra_usb_modem *modem)
468 {
469         const struct platform_device *hc_device =
470             modem->pdata->tegra_ehci_device;
471         struct platform_device *pdev;
472         int val;
473
474         pdev = platform_device_alloc(hc_device->name, hc_device->id);
475         if (!pdev)
476                 return NULL;
477
478         val = platform_device_add_resources(pdev, hc_device->resource,
479                                             hc_device->num_resources);
480         if (val)
481                 goto error;
482
483         pdev->dev.dma_mask = hc_device->dev.dma_mask;
484         pdev->dev.coherent_dma_mask = hc_device->dev.coherent_dma_mask;
485
486         val = platform_device_add_data(pdev, modem->pdata->tegra_ehci_pdata,
487                                        sizeof(struct tegra_usb_platform_data));
488         if (val)
489                 goto error;
490
491         val = platform_device_add(pdev);
492         if (val)
493                 goto error;
494
495         return pdev;
496
497 error:
498         pr_err("%s: err %d\n", __func__, val);
499         platform_device_put(pdev);
500         return NULL;
501 }
502
503 /* unload USB host controller */
504 static void tegra_usb_host_unregister(struct platform_device *pdev)
505 {
506         platform_device_unregister(pdev);
507 }
508
509 static ssize_t show_usb_host(struct device *dev,
510                              struct device_attribute *attr, char *buf)
511 {
512         struct tegra_usb_modem *modem = dev_get_drvdata(dev);
513
514         return sprintf(buf, "%d\n", (modem->hc) ? 1 : 0);
515 }
516
517 static ssize_t load_unload_usb_host(struct device *dev,
518                                     struct device_attribute *attr,
519                                     const char *buf, size_t count)
520 {
521         struct tegra_usb_modem *modem = dev_get_drvdata(dev);
522         int host;
523
524         if (sscanf(buf, "%d", &host) != 1 || host < 0 || host > 1)
525                 return -EINVAL;
526
527         pr_info("%s USB host\n", (host) ? "load" : "unload");
528
529         mutex_lock(&modem->hc_lock);
530         if (host) {
531                 if (!modem->hc)
532                         modem->hc = tegra_usb_host_register(modem);
533         } else {
534                 if (modem->hc) {
535                         tegra_usb_host_unregister(modem->hc);
536                         modem->hc = NULL;
537                 }
538         }
539         mutex_unlock(&modem->hc_lock);
540
541         return count;
542 }
543
544 static DEVICE_ATTR(load_host, S_IRUSR | S_IWUSR, show_usb_host,
545                    load_unload_usb_host);
546
547 #define EDP_INT_ATTR(field)                                             \
548 static ssize_t                                                          \
549 field ## _show(struct device *pdev, struct device_attribute *attr,      \
550                char *buf)                                               \
551 {                                                                       \
552         struct tegra_usb_modem *modem = dev_get_drvdata(pdev);          \
553                                                                         \
554         if (!modem)                                                     \
555                 return -EAGAIN;                                         \
556                                                                         \
557         return sprintf(buf, "%d\n", modem->field);                      \
558 }                                                                       \
559 static DEVICE_ATTR(field, S_IRUSR, field ## _show, NULL);
560
561 EDP_INT_ATTR(i_breach_ppm);
562 EDP_INT_ATTR(i_thresh_3g_adjperiod);
563 EDP_INT_ATTR(i_thresh_lte_adjperiod);
564
565 static ssize_t i_max_show(struct device *pdev, struct device_attribute *attr,
566                           char *buf)
567 {
568         struct tegra_usb_modem *modem = dev_get_drvdata(pdev);
569         int i = 0;
570         int count = 0;
571
572         mutex_lock(&modem->edp_lock);
573         for (i = 0; i < MAX_MODEM_EDP_STATES; i++) {
574                 if (modem->modem_edp_states[i] <= 0)
575                         break;
576
577                 count += sprintf(&buf[count], "%d ",
578                                  modem->modem_edp_states[i]);
579         }
580         mutex_unlock(&modem->edp_lock);
581
582         count += sprintf(&buf[count], "\n");
583
584         return count;
585 }
586
587 static ssize_t i_max_store(struct device *pdev, struct device_attribute *attr,
588                            const char *buff, size_t size)
589 {
590         struct tegra_usb_modem *modem;
591         char *s, *state_i_max, buf[50];
592         unsigned int num_states = 0;
593         struct edp_manager *mgr;
594         int ret;
595
596         modem = (struct tegra_usb_modem *)dev_get_drvdata(pdev);
597
598         mutex_lock(&modem->edp_lock);
599
600         /* client should only be registered once per modem boot */
601         if (modem->edp_client_registered) {
602                 pr_err("modem edp client already registered\n");
603                 ret = -EBUSY;
604                 goto done;
605         }
606
607         memset(modem->modem_edp_states, 0, sizeof(modem->modem_edp_states));
608         memset(&modem->modem_edp_client, 0, sizeof(modem->modem_edp_client));
609
610         /* retrieve max current for supported states */
611         strlcpy(buf, buff, sizeof(buf));
612         s = strim(buf);
613         while (s && (num_states < MAX_MODEM_EDP_STATES)) {
614                 state_i_max = strsep(&s, ",");
615                 ret = kstrtoul(state_i_max, 10,
616                         (unsigned long *)&modem->modem_edp_states[num_states]);
617                 if (ret) {
618                         pr_err("invalid modem state-current setting\n");
619                         goto done;
620                 }
621                 num_states++;
622         }
623
624         if (s && (num_states == MAX_MODEM_EDP_STATES)) {
625                 pr_err("number of modem EDP states exceeded max\n");
626                 ret = -EINVAL;
627                 goto done;
628         }
629
630         strncpy(modem->modem_edp_client.name, "modem", EDP_NAME_LEN);
631         modem->modem_edp_client.name[EDP_NAME_LEN - 1] = '\0';
632         modem->modem_edp_client.states = modem->modem_edp_states;
633         modem->modem_edp_client.num_states = num_states;
634         modem->modem_edp_client.e0_index = 0;
635         modem->modem_edp_client.priority = EDP_MAX_PRIO;
636
637         mgr = edp_get_manager(modem->edp_manager_name);
638         if (!mgr) {
639                 dev_err(pdev, "can't get edp manager\n");
640                 ret = -EINVAL;
641                 goto done;
642         }
643
644         /* register modem client */
645         ret = edp_register_client(mgr, &modem->modem_edp_client);
646         if (ret) {
647                 dev_err(pdev, "unable to register modem edp client\n");
648                 goto done;
649         }
650         modem->edp_client_registered = 1;
651
652         /* unregister modem_boot_client */
653         ret = edp_unregister_client(modem->modem_boot_edp_client);
654         if (ret) {
655                 dev_err(pdev, "unable to register modem boot edp client\n");
656                 goto done;
657         }
658
659         modem->edp_boot_client_registered = 0;
660         ret = size;
661
662 done:
663         mutex_unlock(&modem->edp_lock);
664         return ret;
665 }
666 static DEVICE_ATTR(i_max, S_IRUSR | S_IWUSR, i_max_show, i_max_store);
667
668 static ssize_t request_store(struct device *pdev, struct device_attribute *attr,
669                              const char *buff, size_t size)
670 {
671         struct tegra_usb_modem *modem = dev_get_drvdata(pdev);
672         struct edp_client *c;
673         unsigned int id;
674         int ret;
675
676         if (sscanf(buff, "%u", &id) != 1)
677                 return -EINVAL;
678
679         if (!modem->edp_client_registered)
680                 return -EINVAL;
681
682         c = &modem->modem_edp_client;
683         if (id >= c->num_states)
684                 return -EINVAL;
685
686         ret = edp_update_client_request(c, id, NULL);
687         if (ret)
688                 dev_err(pdev, "state update to %u failed\n", id);
689         else
690                 ret = size;
691
692         return ret;
693 }
694 static DEVICE_ATTR(request, S_IWUSR, NULL, request_store);
695
696 static ssize_t threshold_store(struct device *pdev,
697                                struct device_attribute *attr,
698                                const char *buff, size_t size)
699 {
700         struct tegra_usb_modem *modem = dev_get_drvdata(pdev);
701         unsigned int tv;
702         int ret;
703
704         if (sscanf(buff, "%u", &tv) != 1)
705                 return -EINVAL;
706
707         if (!modem->edp_client_registered)
708                 return -EINVAL;
709
710         ret = edp_update_loan_threshold(&modem->modem_edp_client, tv);
711         if (ret)
712                 dev_err(pdev, "threshold update to %u failed\n", tv);
713         else
714                 ret = size;
715
716         return ret;
717 }
718 DEVICE_ATTR(threshold, S_IWUSR, NULL, threshold_store);
719
720 static struct device_attribute *edp_attributes[] = {
721         &dev_attr_i_breach_ppm,
722         &dev_attr_i_thresh_3g_adjperiod,
723         &dev_attr_i_thresh_lte_adjperiod,
724         &dev_attr_i_max,
725         &dev_attr_request,
726         &dev_attr_threshold,
727         NULL
728 };
729
730 static struct tegra_usb_phy_platform_ops tegra_usb_modem_platform_ops = {
731         .post_remote_wakeup = tegra_usb_modem_post_remote_wakeup,
732 };
733
734 static int mdm_init(struct tegra_usb_modem *modem, struct platform_device *pdev)
735 {
736         struct tegra_usb_modem_power_platform_data *pdata =
737             pdev->dev.platform_data;
738         int ret = 0;
739         struct edp_manager *mgr;
740         struct device_attribute **edp_attrs;
741         struct device_attribute *attr;
742
743         modem->pdata = pdata;
744         modem->pdev = pdev;
745
746         if (pdata->modem_boot_edp_client && pdata->edp_manager_name) {
747                 mutex_init(&modem->edp_lock);
748
749                 /* register modem boot client */
750                 modem->edp_manager_name = pdata->edp_manager_name;
751                 mgr = edp_get_manager(pdata->edp_manager_name);
752                 if (!mgr) {
753                         dev_err(&pdev->dev, "can't get edp manager\n");
754                         goto error;
755                 }
756
757                 modem->modem_boot_edp_client = pdata->modem_boot_edp_client;
758                 ret = edp_register_client(mgr, modem->modem_boot_edp_client);
759                 if (ret) {
760                         dev_err(&pdev->dev,
761                                 "unable to register modem boot edp client\n");
762                         goto error;
763                 }
764
765                 /* request E0 */
766                 ret = edp_update_client_request(modem->modem_boot_edp_client,
767                                                 0, NULL);
768                 if (ret) {
769                         dev_err(&pdev->dev,
770                                 "unable to set e0 state\n");
771                         goto error;
772                 }
773
774                 modem->edp_boot_client_registered = 1;
775
776                 modem->i_breach_ppm = pdata->i_breach_ppm;
777                 modem->i_thresh_3g_adjperiod = pdata->i_thresh_3g_adjperiod;
778                 modem->i_thresh_lte_adjperiod = pdata->i_thresh_lte_adjperiod;
779
780                 edp_attrs = edp_attributes;
781                 while ((attr = *edp_attrs++)) {
782                         ret = device_create_file(&pdev->dev, attr);
783                         if (ret) {
784                                 dev_err(&pdev->dev, "can't create sysfs file\n");
785                                 goto error;
786                         }
787                 }
788
789                 INIT_WORK(&modem->edp_work, edp_work);
790
791                 modem->edp_initialized = 1;
792         }
793
794         /* get modem operations from platform data */
795         modem->ops = (const struct tegra_modem_operations *)pdata->ops;
796
797         if (modem->ops) {
798                 /* modem init */
799                 if (modem->ops->init) {
800                         ret = modem->ops->init();
801                         if (ret)
802                                 return ret;
803                 }
804
805                 /* start modem */
806                 if (modem->ops->start)
807                         modem->ops->start();
808         }
809
810         /* create sysfs node to load/unload host controller */
811         ret = device_create_file(&pdev->dev, &dev_attr_load_host);
812         if (ret) {
813                 dev_err(&pdev->dev, "can't create sysfs file\n");
814                 goto error;
815         }
816         modem->sysfs_file_created = 1;
817         modem->capability = TEGRA_USB_HOST_RELOAD;
818         mutex_init(&(modem->lock));
819         mutex_init(&modem->hc_lock);
820         wake_lock_init(&modem->wake_lock, WAKE_LOCK_SUSPEND, "mdm_lock");
821         if (pdev->id >= 0)
822                 dev_set_name(&pdev->dev, "MDM%d", pdev->id);
823         else
824                 dev_set_name(&pdev->dev, "MDM");
825
826         /* create work queue platform_driver_registe */
827         modem->wq = create_workqueue("tegra_usb_mdm_queue");
828         INIT_DELAYED_WORK(&modem->recovery_work, tegra_usb_modem_recovery);
829         INIT_WORK(&modem->host_load_work, tegra_usb_host_load);
830         INIT_WORK(&modem->host_unload_work, tegra_usb_host_unload);
831         INIT_WORK(&modem->cpu_boost_work, cpu_freq_boost);
832         INIT_DELAYED_WORK(&modem->cpu_unboost_work, cpu_freq_unboost);
833
834         pm_qos_add_request(&modem->cpu_boost_req, PM_QOS_CPU_FREQ_MIN,
835                            PM_QOS_DEFAULT_VALUE);
836
837         /* if wake gpio is not specified we rely on native usb remote wake */
838         if (gpio_is_valid(pdata->wake_gpio)) {
839                 /* request remote wakeup irq from platform data */
840                 ret = mdm_request_wakeable_irq(modem,
841                                                tegra_usb_modem_wake_thread,
842                                                pdata->wake_gpio,
843                                                pdata->wake_irq_flags,
844                                                "mdm_wake", &modem->wake_irq);
845                 if (ret) {
846                         dev_err(&pdev->dev, "request wake irq error\n");
847                         goto error;
848                 }
849         } else {
850                 modem->pdata->tegra_ehci_pdata->ops =
851                                                 &tegra_usb_modem_platform_ops;
852         }
853
854
855         if (gpio_is_valid(pdata->boot_gpio)) {
856                 /* request boot irq from platform data */
857                 ret = mdm_request_wakeable_irq(modem,
858                                                tegra_usb_modem_boot_thread,
859                                                pdata->boot_gpio,
860                                                pdata->boot_irq_flags,
861                                                "mdm_boot", &modem->boot_irq);
862                 if (ret) {
863                         dev_err(&pdev->dev, "request boot irq error\n");
864                         goto error;
865                 }
866         } else {
867                 dev_err(&pdev->dev, "boot irq not specified\n");
868                 goto error;
869         }
870
871         modem->pm_notifier.notifier_call = mdm_pm_notifier;
872         modem->usb_notifier.notifier_call = mdm_usb_notifier;
873
874         usb_register_notify(&modem->usb_notifier);
875         register_pm_notifier(&modem->pm_notifier);
876
877         return ret;
878 error:
879         if (modem->sysfs_file_created)
880                 device_remove_file(&pdev->dev, &dev_attr_load_host);
881
882         if (modem->wake_irq) {
883                 disable_irq_wake(modem->wake_irq);
884                 free_irq(modem->wake_irq, modem);
885         }
886
887         if (modem->boot_irq) {
888                 disable_irq_wake(modem->boot_irq);
889                 free_irq(modem->boot_irq, modem);
890         }
891
892         if (modem->edp_initialized) {
893                 edp_attrs = edp_attributes;
894                 while ((attr = *edp_attrs++))
895                         device_remove_file(&pdev->dev, attr);
896         }
897
898         if (modem->edp_boot_client_registered)
899                 edp_unregister_client(modem->modem_boot_edp_client);
900
901         return ret;
902 }
903
904 static int tegra_usb_modem_probe(struct platform_device *pdev)
905 {
906         struct tegra_usb_modem_power_platform_data *pdata =
907             pdev->dev.platform_data;
908         struct tegra_usb_modem *modem;
909         int ret = 0;
910
911         if (!pdata) {
912                 dev_dbg(&pdev->dev, "platform_data not available\n");
913                 return -EINVAL;
914         }
915
916         modem = kzalloc(sizeof(struct tegra_usb_modem), GFP_KERNEL);
917         if (!modem) {
918                 dev_dbg(&pdev->dev, "failed to allocate memory\n");
919                 return -ENOMEM;
920         }
921
922         ret = mdm_init(modem, pdev);
923         if (ret) {
924                 kfree(modem);
925                 return ret;
926         }
927
928         dev_set_drvdata(&pdev->dev, modem);
929
930         return ret;
931 }
932
933 static int __exit tegra_usb_modem_remove(struct platform_device *pdev)
934 {
935         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
936         struct device_attribute **edp_attrs;
937         struct device_attribute *attr;
938
939         unregister_pm_notifier(&modem->pm_notifier);
940         usb_unregister_notify(&modem->usb_notifier);
941
942         if (modem->wake_irq) {
943                 disable_irq_wake(modem->wake_irq);
944                 free_irq(modem->wake_irq, modem);
945         }
946
947         if (modem->boot_irq) {
948                 disable_irq_wake(modem->boot_irq);
949                 free_irq(modem->boot_irq, modem);
950         }
951
952         if (modem->sysfs_file_created)
953                 device_remove_file(&pdev->dev, &dev_attr_load_host);
954
955         if (modem->edp_initialized) {
956                 cancel_work_sync(&modem->edp_work);
957
958                 edp_attrs = edp_attributes;
959                 while ((attr = *edp_attrs++))
960                         device_remove_file(&pdev->dev, attr);
961         }
962
963         if (modem->edp_boot_client_registered)
964                 edp_unregister_client(modem->modem_boot_edp_client);
965
966         if (modem->edp_client_registered)
967                 edp_unregister_client(&modem->modem_edp_client);
968
969         cancel_delayed_work_sync(&modem->recovery_work);
970         cancel_work_sync(&modem->host_load_work);
971         cancel_work_sync(&modem->host_unload_work);
972         cancel_work_sync(&modem->cpu_boost_work);
973         cancel_delayed_work_sync(&modem->cpu_unboost_work);
974         destroy_workqueue(modem->wq);
975
976         pm_qos_remove_request(&modem->cpu_boost_req);
977
978         kfree(modem);
979         return 0;
980 }
981
982 static void tegra_usb_modem_shutdown(struct platform_device *pdev)
983 {
984         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
985
986         if (modem->ops && modem->ops->stop)
987                 modem->ops->stop();
988 }
989
990 #ifdef CONFIG_PM
991 static int tegra_usb_modem_suspend(struct platform_device *pdev,
992                                    pm_message_t state)
993 {
994         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
995
996         /* send L3 hint to modem */
997         if (modem->ops && modem->ops->suspend)
998                 modem->ops->suspend();
999         return 0;
1000 }
1001
1002 static int tegra_usb_modem_resume(struct platform_device *pdev)
1003 {
1004         struct tegra_usb_modem *modem = platform_get_drvdata(pdev);
1005
1006         /* send L3->L0 hint to modem */
1007         if (modem->ops && modem->ops->resume)
1008                 modem->ops->resume();
1009         return 0;
1010 }
1011 #endif
1012
1013 static struct platform_driver tegra_usb_modem_power_driver = {
1014         .driver = {
1015                    .name = "tegra_usb_modem_power",
1016                    .owner = THIS_MODULE,
1017                    },
1018         .probe = tegra_usb_modem_probe,
1019         .remove = __exit_p(tegra_usb_modem_remove),
1020         .shutdown = tegra_usb_modem_shutdown,
1021 #ifdef CONFIG_PM
1022         .suspend = tegra_usb_modem_suspend,
1023         .resume = tegra_usb_modem_resume,
1024 #endif
1025 };
1026
1027 static int __init tegra_usb_modem_power_init(void)
1028 {
1029         return platform_driver_register(&tegra_usb_modem_power_driver);
1030 }
1031
1032 module_init(tegra_usb_modem_power_init);
1033
1034 static void __exit tegra_usb_modem_power_exit(void)
1035 {
1036         platform_driver_unregister(&tegra_usb_modem_power_driver);
1037 }
1038
1039 module_exit(tegra_usb_modem_power_exit);
1040
1041 MODULE_DESCRIPTION("Tegra usb modem power management driver");
1042 MODULE_LICENSE("GPL");