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