1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
|
/*
* drivers/misc/tegra-baseband/bb-oem1.c
*
* Copyright (c) 2012, NVIDIA Corporation.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <linux/resource.h>
#include <linux/platform_device.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/err.h>
#include <linux/device.h>
#include <linux/usb.h>
#include <linux/wakelock.h>
#include <linux/suspend.h>
#include <linux/platform_data/tegra_usb.h>
#include <linux/regulator/consumer.h>
#include <asm/mach-types.h>
#include <asm/mach/arch.h>
#include <mach/tegra-bb-power.h>
#include <mach/usb_phy.h>
#include "bb-power.h"
#include "bb-oem1.h"
#define WAKE_TIMEOUT_MS 50
static struct tegra_bb_gpio_data bb_gpios[] = {
{ { GPIO_INVALID, GPIOF_OUT_INIT_LOW, "MDM4_RST" }, true },
{ { GPIO_INVALID, GPIOF_OUT_INIT_LOW, "MDM4_ON" }, true },
{ { GPIO_INVALID, GPIOF_OUT_INIT_LOW, "MDM4_USB_AWR" }, true },
{ { GPIO_INVALID, GPIOF_IN, "MDM4_USB_CWR" }, true },
{ { GPIO_INVALID, GPIOF_IN, "MDM4_SPARE" }, true },
{ { GPIO_INVALID, GPIOF_IN, "MDM4_WDI" }, true },
{ { GPIO_INVALID, 0, NULL }, false }, /* End of table */
};
static struct sdata bb_sdata;
static struct opsdata bb_opdata;
static struct locks bb_locks;
static struct regulator *hsic_reg;
static bool ehci_registered;
static int dlevel;
static int gpio_wait_timeout(int gpio, int value, int timeout_msec)
{
int count;
for (count = 0; count < timeout_msec; ++count) {
if (gpio_get_value(gpio) == value)
return 0;
mdelay(1);
}
return -1;
}
static void bb_setdata(struct opsdata *data)
{
unsigned long flags;
spin_lock_irqsave(&bb_locks.lock, flags);
if (data) {
if (data->powerstate != bb_opdata.powerstate)
bb_opdata.powerstate = data->powerstate;
if (data->usbdev != bb_opdata.usbdev)
bb_opdata.usbdev = data->usbdev;
}
spin_unlock_irqrestore(&bb_locks.lock, flags);
if (data->powerstate == BBSTATE_UNKNOWN) {
if (!wake_lock_active(&bb_locks.wlock)) {
wake_lock(&bb_locks.wlock);
if (dlevel & DLEVEL_MISC)
pr_info("%s: Taking wakelock.\n", __func__);
} else
pr_warn("%s: Active wakelock in UNK\n", __func__);
} else {
if (wake_lock_active(&bb_locks.wlock)) {
wake_unlock(&bb_locks.wlock);
if (dlevel & DLEVEL_MISC)
pr_info("%s: Releasing wakelock.\n", __func__);
}
}
}
static void bb_getdata(struct opsdata *data)
{
unsigned long flags;
spin_lock_irqsave(&bb_locks.lock, flags);
if (data) {
data->powerstate = bb_opdata.powerstate;
data->usbdev = bb_opdata.usbdev;
}
spin_unlock_irqrestore(&bb_locks.lock, flags);
}
static int bb_getpowerstate(void)
{
struct opsdata data;
bb_getdata(&data);
return data.powerstate;
}
static void bb_setpowerstate(int status)
{
struct opsdata data;
bb_getdata(&data);
data.powerstate = status;
bb_setdata(&data);
}
static bool bb_crashed(void)
{
bool flag;
flag = ((gpio_get_value(bb_sdata.gpio_wdi)) ? false : true);
return flag;
}
static bool bb_get_cwr(void)
{
return gpio_get_value(bb_sdata.gpio_cwr);
}
static void bb_set_awr(int value)
{
gpio_set_value(bb_sdata.gpio_awr, value);
}
static void bb_gpio_stat(void)
{
pr_info("%s: AWR:%d, CWR:%d, WDI:%d.\n", __func__,
gpio_get_value(bb_sdata.gpio_awr),
gpio_get_value(bb_sdata.gpio_cwr),
gpio_get_value(bb_sdata.gpio_wdi));
}
static int apup_handshake(bool checkresponse)
{
int retval = 0;
/* Signal AP ready - Drive USB_AWR high. */
bb_set_awr(1);
if (checkresponse) {
/* Wait for CP ack - by driving USB_CWR high. */
if (gpio_wait_timeout(bb_sdata.gpio_cwr, 1,
WAKE_TIMEOUT_MS) != 0) {
pr_err("%s: Error: Timeout waiting for modem ack.\n",
__func__);
retval = -1;
}
}
return retval;
}
static void apdown_handshake(void)
{
/* Signal AP going down to modem - Drive USB_AWR low. */
/* No need to wait for a CP response */
bb_set_awr(0);
}
static void pre_l2_suspend(void)
{
bb_setpowerstate(BBSTATE_L02L2);
}
static void l2_suspend(void)
{
int modemstate = bb_getpowerstate();
if (dlevel & DLEVEL_PM)
pr_info("%s.\n", __func__);
if (modemstate != BBSTATE_L02L2) {
pr_err("%s: Error. Unexp modemstate %x. Should be %d.\n",
__func__, modemstate, BBSTATE_L02L2);
return;
}
bb_setpowerstate(BBSTATE_L2);
}
static void l2_resume(void)
{
int modemstate = bb_getpowerstate();
bool resumeok = false;
if (dlevel & DLEVEL_PM)
pr_info("%s.\n", __func__);
/* Gets called for two cases :
a) L2 resume.
b) bus resume phase of L3 resume. */
switch (modemstate) {
case BBSTATE_L2:
resumeok = true;
break;
case BBSTATE_L3:
if (apup_handshake(true) == 0)
resumeok = true;
else {
bb_gpio_stat();
pr_err("%s: Error. Modem wakeup from L3 failed.\n",
__func__);
/* TBD: Add code to unregister ehci. */
}
break;
default:
pr_err("%s: Error. Unexp modemstate %x. Should be %d or %d.\n",
__func__, modemstate, BBSTATE_L2, BBSTATE_L3);
break;
}
if (resumeok)
bb_setpowerstate(BBSTATE_L0);
}
static void phy_ready(void)
{
int modemstate = bb_getpowerstate();
if (dlevel & DLEVEL_PM)
pr_info("%s.\n", __func__);
switch (modemstate) {
case BBSTATE_UNKNOWN:
/* Wait for CP to indicate ready - by driving USB_CWR high. */
if (gpio_wait_timeout(bb_sdata.gpio_cwr, 1, 10) != 0) {
pr_info("%s: Timeout 4 modem ready. Maybe 1st enum ?\n",
__func__);
/* For first enumeration don't drive AWR high */
break;
}
/* Signal AP ready - Drive USB_AWR high. */
pr_info("%s : Driving AWR high.\n", __func__);
bb_set_awr(1);
break;
default:
pr_err("%s: Error. Unexp modemstate %x. Should be %d.\n",
__func__, modemstate, BBSTATE_UNKNOWN);
break;
}
}
static void pre_phy_off(void)
{
if (dlevel & DLEVEL_PM)
pr_info("%s.\n", __func__);
/* Signal AP going down */
apdown_handshake();
/* Gets called for two cases :
a) L3 suspend.
b) EHCI unregister. */
if (bb_getpowerstate() == BBSTATE_L2)
bb_setpowerstate(BBSTATE_L3);
}
static int l3_suspend(void)
{
int pwrstate = bb_getpowerstate();
bool wakeup_detected = bb_get_cwr();
bool crashed = bb_crashed();
if (dlevel & DLEVEL_PM)
pr_info("%s.\n", __func__);
/* If modem state during system suspend is not L3 (crashed)
or modem is initiating a wakeup, abort system suspend. */
if ((pwrstate != BBSTATE_L3) || wakeup_detected || crashed) {
if (pwrstate != BBSTATE_L3)
pr_err("%s: Unexp modemstate %x. Should be %d.\n",
__func__, pwrstate, BBSTATE_L3);
if (wakeup_detected)
pr_info("%s : CWR high.\n", __func__);
if (crashed)
pr_info("%s : WDI low.\n", __func__);
pr_info("%s: Aborting suspend.\n", __func__);
return 1;
}
return 0;
}
static int l3_suspend_noirq(void)
{
bool wakeup_detected = bb_get_cwr();
bool crashed = bb_crashed();
/* If modem is initiating a wakeup, or it had crashed
abort system suspend. */
if (wakeup_detected || crashed) {
pr_info("%s: Aborting suspend.\n", __func__);
return 1;
}
return 0;
}
static int l3_resume(void)
{
return 0;
}
static int l3_resume_noirq(void)
{
return 0;
}
static irqreturn_t bb_wake_irq(int irq, void *dev_id)
{
struct opsdata data;
struct usb_interface *iface;
int cwrlevel = bb_get_cwr();
bool pwrstate_l2 = false;
bb_getdata(&data);
pwrstate_l2 = ((data.powerstate == BBSTATE_L02L2) ||
(data.powerstate == BBSTATE_L2));
if (cwrlevel && pwrstate_l2) {
if (dlevel & DLEVEL_PM)
pr_info("%s: Modem wakeup request from L2.\n",
__func__);
if (data.usbdev) {
usb_lock_device(data.usbdev);
iface = usb_ifnum_to_if(data.usbdev, 0);
if (iface) {
/* Resume usb host activity. */
usb_autopm_get_interface(iface);
usb_autopm_put_interface_no_suspend(iface);
}
usb_unlock_device(data.usbdev);
}
}
if (!cwrlevel && data.powerstate == BBSTATE_UNKNOWN && data.usbdev) {
data.powerstate = BBSTATE_L0;
bb_setdata(&data);
if (dlevel & DLEVEL_PM)
pr_info("%s: Network interface up.\n", __func__);
}
return IRQ_HANDLED;
}
static int bb_power(int code)
{
int retval = 0;
switch (code) {
case PWRSTATE_L2L3:
retval = l3_suspend();
break;
case PWRSTATE_L2L3_NOIRQ:
retval = l3_suspend_noirq();
break;
case PWRSTATE_L3L0_NOIRQ:
retval = l3_resume_noirq();
break;
case PWRSTATE_L3L0:
retval = l3_resume();
break;
default:
break;
}
return retval;
}
static int bb_ehci_customize(struct tegra_bb_pdata *pdata)
{
struct platform_device *pdev;
struct tegra_usb_platform_data *usb_pdata;
struct tegra_usb_phy_platform_ops *pops;
if (pdata && pdata->device) {
pdev = pdata->device;
usb_pdata = (struct tegra_usb_platform_data *)
pdev->dev.platform_data;
pops = (struct tegra_usb_phy_platform_ops *)
usb_pdata->ops;
/* Register PHY platform callbacks */
pops->pre_suspend = pre_l2_suspend;
pops->post_suspend = l2_suspend;
pops->pre_resume = l2_resume;
pops->port_power = phy_ready;
pops->pre_phy_off = pre_phy_off;
/* Override required settings */
usb_pdata->u_data.host.power_off_on_suspend = 0;
} else {
pr_err("%s: Error. Invalid platform data.\n", __func__);
return 0;
}
return 1;
}
static int bb_sysfs_load(struct device *dev, int value)
{
struct tegra_bb_pdata *pdata;
static struct platform_device *ehci_device;
struct opsdata data;
if (dlevel & DLEVEL_SYS_CB)
pr_info("%s: Called with value : %d\n", __func__, value);
if (value > 1 || (!ehci_registered && !value) ||
(ehci_registered && value)) {
/* Supported values are 0/1. */
pr_err("%s: Error. Invalid data. Exiting.\n", __func__);
return -1;
}
pdata = (struct tegra_bb_pdata *) dev->platform_data;
if (value) {
/* Register ehci controller */
ehci_device = pdata->ehci_register(pdata->device);
if (ehci_device == NULL) {
pr_err("%s: Error. ehci register failed.\n",
__func__);
return -1;
}
ehci_registered = true;
} else {
/* Mark usb device invalid */
data.usbdev = NULL;
data.powerstate = BBSTATE_UNKNOWN;
bb_setdata(&data);
ehci_registered = false;
/* Unregister ehci controller */
if (ehci_device != NULL)
pdata->ehci_unregister(&ehci_device);
/* Signal AP going down */
apdown_handshake();
}
return 0;
}
static int bb_sysfs_dlevel(struct device *dev, int value)
{
if (dlevel & DLEVEL_SYS_CB)
pr_info("%s: Called with value : %d\n", __func__, value);
dlevel = value;
return 0;
}
static int bb_usbnotify(struct usb_device *udev, bool registered)
{
struct opsdata data;
data.powerstate = BBSTATE_UNKNOWN;
if (registered) {
data.usbdev = udev;
if (dlevel & DLEVEL_MISC)
pr_info("%s: Modem attached.\n", __func__);
} else {
data.usbdev = NULL;
if (dlevel & DLEVEL_MISC)
pr_info("%s: Modem detached.\n", __func__);
}
bb_setdata(&data);
return 0;
}
#ifdef CONFIG_PM_SLEEP
static int bb_pmnotify(unsigned long event)
{
int pwrstate = bb_getpowerstate();
int retval = NOTIFY_OK;
if (dlevel & DLEVEL_PM)
pr_info("%s: PM notification %ld.\n", __func__, event);
switch (event) {
case PM_SUSPEND_PREPARE:
if (pwrstate == BBSTATE_UNKNOWN) {
pr_err("%s: Suspend with pwrstate=%d. Aborting suspend.\n",
__func__, pwrstate);
retval = NOTIFY_BAD;
}
break;
case PM_POST_SUSPEND:
break;
default:
retval = NOTIFY_DONE;
break;
}
return retval;
}
#endif
static struct tegra_bb_gpio_irqdata bb_gpioirqs[] = {
{ GPIO_INVALID, "tegra_bb_wake", bb_wake_irq,
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, true, NULL },
{ GPIO_INVALID, "tegra_bb_wdi", NULL,
IRQF_TRIGGER_NONE, true, NULL },
{ GPIO_INVALID, NULL, NULL, 0, NULL }, /* End of table */
};
static struct tegra_bb_power_gdata bb_gdata = {
.gpio = bb_gpios,
.gpioirq = bb_gpioirqs,
};
static struct tegra_bb_power_mdata bb_mdata = {
.vid = 0x045B,
.pid = 0x020F,
.wake_capable = true,
.autosuspend_ready = true,
};
static struct tegra_bb_power_data bb_data = {
.gpio_data = &bb_gdata,
.modem_data = &bb_mdata,
};
static void *bb_init(void *pdata)
{
struct tegra_bb_pdata *platdata = (struct tegra_bb_pdata *) pdata;
union tegra_bb_gpio_id *id = platdata->id;
struct opsdata data;
/* Fill the gpio ids allocated by hardware */
bb_gpios[0].data.gpio = id->oem1.reset;
bb_gpios[1].data.gpio = id->oem1.pwron;
bb_gpios[2].data.gpio = id->oem1.awr;
bb_gpios[3].data.gpio = id->oem1.cwr;
bb_gpios[4].data.gpio = id->oem1.spare;
bb_gpios[5].data.gpio = id->oem1.wdi;
bb_gpioirqs[0].id = id->oem1.cwr;
bb_gpioirqs[1].id = id->oem1.wdi;
if (!platdata->ehci_register || !platdata->ehci_unregister) {
pr_err("%s - Error: ehci reg/unreg functions missing.\n"
, __func__);
return 0;
}
bb_sdata.gpio_awr = bb_gpios[2].data.gpio;
bb_sdata.gpio_cwr = bb_gpios[3].data.gpio;
bb_sdata.gpio_wdi = bb_gpios[5].data.gpio;
if (bb_sdata.gpio_awr == GPIO_INVALID ||
bb_sdata.gpio_cwr == GPIO_INVALID ||
bb_sdata.gpio_wdi == GPIO_INVALID) {
pr_err("%s: Error. Invalid gpio data.\n", __func__);
return 0;
}
/* Customize PHY setup/callbacks */
if (!bb_ehci_customize(platdata))
return 0;
/* Board specific regulator init */
if (platdata->regulator) {
hsic_reg = regulator_get(NULL,
(const char *)platdata->regulator);
if (IS_ERR_OR_NULL(hsic_reg)) {
pr_err("%s: Error. regulator_get failed.\n",
__func__);
return 0;
}
if (regulator_enable(hsic_reg) < 0) {
pr_err("%s: Error. regulator_enable failed.\n",
__func__);
return 0;
}
}
spin_lock_init(&bb_locks.lock);
wake_lock_init(&bb_locks.wlock, WAKE_LOCK_SUSPEND,
"tegra-bb-lock");
dlevel = DLEVEL_INIT;
ehci_registered = false;
data.usbdev = NULL;
data.powerstate = BBSTATE_UNKNOWN;
bb_setdata(&data);
return (void *) &bb_data;
}
static void *bb_deinit(void)
{
/* destroy wake lock */
wake_lock_destroy(&bb_locks.wlock);
return (void *) &bb_data;
}
static struct tegra_bb_callback bb_callbacks = {
.init = bb_init,
.deinit = bb_deinit,
.load = bb_sysfs_load,
.dlevel = bb_sysfs_dlevel,
.usbnotify = bb_usbnotify,
#ifdef CONFIG_PM
.power = bb_power,
#endif
#ifdef CONFIG_PM_SLEEP
.pmnotify = bb_pmnotify,
#endif
};
void *bb_oem1_get_cblist(void)
{
return (void *) &bb_callbacks;
}
|