summaryrefslogtreecommitdiff
path: root/drivers/usb/otg/ab8500-usb.c
blob: d6b71fcfcfee3d930480ca0d9f0e3efaf2acc71b (plain)
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
/*
 * drivers/usb/otg/ab8500_usb.c
 *
 * USB transceiver driver for AB8500 chip
 *
 * Copyright (C) 2010 ST-Ericsson AB
 * Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/usb/otg.h>
#include <linux/slab.h>
#include <linux/notifier.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/mfd/abx500.h>
#include <linux/mfd/abx500/ab8500.h>
#include <linux/mfd/dbx500-prcmu.h>
#include <linux/kernel_stat.h>
#include <linux/pm_qos.h>
#include <linux/wakelock.h>

static struct wake_lock ab8500_musb_wakelock;

#define AB8500_MAIN_WD_CTRL_REG 0x01
#define AB8500_USB_LINE_STAT_REG 0x80
#define AB8500_USB_PHY_CTRL_REG 0x8A
#define AB8500_VBUS_CTRL_REG 0x82
#define AB8500_IT_SOURCE2_REG 0x01
#define AB8500_IT_SOURCE20_REG 0x13
#define AB8500_SRC_INT_USB_HOST 0x04
#define AB8500_SRC_INT_USB_DEVICE 0x80

#define AB8500_BIT_OTG_STAT_ID (1 << 0)
#define AB8500_BIT_PHY_CTRL_HOST_EN (1 << 0)
#define AB8500_BIT_PHY_CTRL_DEVICE_EN (1 << 1)
#define AB8500_BIT_WD_CTRL_ENABLE (1 << 0)
#define AB8500_BIT_WD_CTRL_KICK (1 << 1)
#define AB8500_BIT_VBUS_ENABLE (1 << 0)

#define AB8500_V1x_LINK_STAT_WAIT (HZ/10)
#define AB8500_WD_KICK_DELAY_US 100 /* usec */
#define AB8500_WD_V11_DISABLE_DELAY_US 100 /* usec */
#define AB8500_V20_31952_DISABLE_DELAY_US 100 /* usec */
#define AB8500_WD_V10_DISABLE_DELAY_MS 100 /* ms */

/* Registers in bank 0x11 */
#define AB8500_BANK12_ACCESS	0x00

/* Registers in bank 0x12 */
#define AB8500_USB_PHY_TUNE1	0x05
#define AB8500_USB_PHY_TUNE2	0x06
#define AB8500_USB_PHY_TUNE3	0x07

static struct pm_qos_request usb_pm_qos_latency;
static bool usb_pm_qos_is_latency_0;

#define USB_PROBE_DELAY 1000 /* 1 seconds */
#define USB_LIMIT (200) /* If we have more than 200 irqs per second */

#define PUBLIC_ID_BACKUPRAM1 (U8500_BACKUPRAM1_BASE + 0x0FC0)
#define MAX_USB_SERIAL_NUMBER_LEN 31
#define AB8505_USB_LINE_STAT_REG 0x94

/* Usb line status register */
enum ab8500_usb_link_status {
	USB_LINK_NOT_CONFIGURED_8500 = 0,
	USB_LINK_STD_HOST_NC_8500,
	USB_LINK_STD_HOST_C_NS_8500,
	USB_LINK_STD_HOST_C_S_8500,
	USB_LINK_HOST_CHG_NM_8500,
	USB_LINK_HOST_CHG_HS_8500,
	USB_LINK_HOST_CHG_HS_CHIRP_8500,
	USB_LINK_DEDICATED_CHG_8500,
	USB_LINK_ACA_RID_A_8500,
	USB_LINK_ACA_RID_B_8500,
	USB_LINK_ACA_RID_C_NM_8500,
	USB_LINK_ACA_RID_C_HS_8500,
	USB_LINK_ACA_RID_C_HS_CHIRP_8500,
	USB_LINK_HM_IDGND_8500,
	USB_LINK_RESERVED_8500,
	USB_LINK_NOT_VALID_LINK_8500,
};

enum ab8505_usb_link_status {
	USB_LINK_NOT_CONFIGURED_8505 = 0,
	USB_LINK_STD_HOST_NC_8505,
	USB_LINK_STD_HOST_C_NS_8505,
	USB_LINK_STD_HOST_C_S_8505,
	USB_LINK_CDP_8505,
	USB_LINK_RESERVED0_8505,
	USB_LINK_RESERVED1_8505,
	USB_LINK_DEDICATED_CHG_8505,
	USB_LINK_ACA_RID_A_8505,
	USB_LINK_ACA_RID_B_8505,
	USB_LINK_ACA_RID_C_NM_8505,
	USB_LINK_RESERVED2_8505,
	USB_LINK_RESERVED3_8505,
	USB_LINK_HM_IDGND_8505,
	USB_LINK_CHARGERPORT_NOT_OK_8505,
	USB_LINK_CHARGER_DM_HIGH_8505,
	USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8505,
	USB_LINK_STD_UPSTREAM_NO_IDGNG_NO_VBUS_8505,
	USB_LINK_STD_UPSTREAM_8505,
	USB_LINK_CHARGER_SE1_8505,
	USB_LINK_CARKIT_CHGR_1_8505,
	USB_LINK_CARKIT_CHGR_2_8505,
	USB_LINK_ACA_DOCK_CHGR_8505,
	USB_LINK_SAMSUNG_BOOT_CBL_PHY_EN_8505,
	USB_LINK_SAMSUNG_BOOT_CBL_PHY_DISB_8505,
	USB_LINK_SAMSUNG_UART_CBL_PHY_EN_8505,
	USB_LINK_SAMSUNG_UART_CBL_PHY_DISB_8505,
	USB_LINK_MOTOROLA_FACTORY_CBL_PHY_EN_8505,
};

enum ab8500_usb_mode {
	USB_IDLE = 0,
	USB_PERIPHERAL,
	USB_HOST,
	USB_DEDICATED_CHG
};

struct ab8500_usb {
	struct usb_phy phy;
	struct device *dev;
	struct ab8500 *ab8500;
	int irq_num_id_rise;
	int irq_num_id_fall;
	int irq_num_vbus_rise;
	int irq_num_vbus_fall;
	int irq_num_link_status;
	unsigned vbus_draw;
	struct delayed_work dwork;
	struct work_struct phy_dis_work;
	unsigned long link_status_wait;
	enum ab8500_usb_mode mode;
	struct clk *sysclk;
	struct regulator *v_ape;
	struct regulator *v_musb;
	struct regulator *v_ulpi;
	struct delayed_work work_usb_workaround;
	bool sysfs_flag;
};

static inline struct ab8500_usb *phy_to_ab(struct usb_phy *x)
{
	return container_of(x, struct ab8500_usb, phy);
}

static void ab8500_usb_wd_workaround(struct ab8500_usb *ab)
{
	abx500_set_register_interruptible(ab->dev,
		AB8500_SYS_CTRL2_BLOCK,
		AB8500_MAIN_WD_CTRL_REG,
		AB8500_BIT_WD_CTRL_ENABLE);

	udelay(AB8500_WD_KICK_DELAY_US);

	abx500_set_register_interruptible(ab->dev,
		AB8500_SYS_CTRL2_BLOCK,
		AB8500_MAIN_WD_CTRL_REG,
		(AB8500_BIT_WD_CTRL_ENABLE
		| AB8500_BIT_WD_CTRL_KICK));

	if (!is_ab8500_1p0_or_earlier(ab->ab8500))
		udelay(AB8500_WD_V11_DISABLE_DELAY_US);

	abx500_set_register_interruptible(ab->dev,
		AB8500_SYS_CTRL2_BLOCK,
		AB8500_MAIN_WD_CTRL_REG,
		0);
}

static void ab8500_usb_load(struct work_struct *work)
{
	int cpu;
	unsigned int num_irqs = 0;
	static unsigned int old_num_irqs = UINT_MAX;
	struct delayed_work *work_usb_workaround = to_delayed_work(work);
	struct ab8500_usb *ab = container_of(work_usb_workaround,
				struct ab8500_usb, work_usb_workaround);

	for_each_online_cpu(cpu)
	num_irqs += kstat_irqs_cpu(IRQ_DB8500_USBOTG, cpu);

	if ((num_irqs > old_num_irqs) &&
		(num_irqs - old_num_irqs) > USB_LIMIT) {

		prcmu_qos_update_requirement(PRCMU_QOS_ARM_OPP,
							"usb", 125);
		if (!usb_pm_qos_is_latency_0) {

			pm_qos_add_request(&usb_pm_qos_latency,
						PM_QOS_CPU_DMA_LATENCY, 0);
			usb_pm_qos_is_latency_0 = true;
		}
	} else {

		if (usb_pm_qos_is_latency_0) {

				pm_qos_remove_request(&usb_pm_qos_latency);
				usb_pm_qos_is_latency_0 = false;
		}

		prcmu_qos_update_requirement(PRCMU_QOS_ARM_OPP,
							"usb", 25);
	}
	old_num_irqs = num_irqs;

	schedule_delayed_work_on(0,
				&ab->work_usb_workaround,
				msecs_to_jiffies(USB_PROBE_DELAY));
}

static void ab8500_usb_regulator_ctrl(struct ab8500_usb *ab, bool sel_host,
					bool enable)
{
	int ret = 0, volt = 0;

	if (enable) {
		regulator_enable(ab->v_ape);
		if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
			ret = regulator_set_voltage(ab->v_ulpi,
						1300000, 1350000);
			if (ret < 0)
				dev_err(ab->dev, "Failed to set the Vintcore"
						" to 1.3V, ret=%d\n", ret);
			ret = regulator_set_optimum_mode(ab->v_ulpi,
								28000);
			if (ret < 0)
				dev_err(ab->dev, "Failed to set optimum mode"
						" (ret=%d)\n", ret);

		}
		regulator_enable(ab->v_ulpi);
		if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
			volt = regulator_get_voltage(ab->v_ulpi);
			if ((volt != 1300000) && (volt != 1350000))
					dev_err(ab->dev, "Vintcore is not"
							" set to 1.3V"
							" volt=%d\n", volt);
		}
		regulator_enable(ab->v_musb);

	} else {
		regulator_disable(ab->v_musb);
		regulator_disable(ab->v_ulpi);
		regulator_disable(ab->v_ape);
	}
}


static void ab8500_usb_phy_enable(struct ab8500_usb *ab, bool sel_host)
{
	u8 bit;
	bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
			AB8500_BIT_PHY_CTRL_DEVICE_EN;

	wake_lock(&ab8500_musb_wakelock);

	clk_enable(ab->sysclk);

	ab8500_usb_regulator_ctrl(ab, sel_host, true);

	prcmu_qos_update_requirement(PRCMU_QOS_APE_OPP,
				(char *)dev_name(ab->dev), 100);

	schedule_delayed_work_on(0,
					&ab->work_usb_workaround,
					msecs_to_jiffies(USB_PROBE_DELAY));

	abx500_mask_and_set_register_interruptible(ab->dev,
				AB8500_USB,
				AB8500_USB_PHY_CTRL_REG,
				bit,
				bit);

}

static void ab8500_usb_wd_linkstatus(struct ab8500_usb *ab,u8 bit)
{
	/* Workaround for v2.0 bug # 31952 */
	if (is_ab8500_2p0(ab->ab8500)) {
		abx500_mask_and_set_register_interruptible(ab->dev,
					AB8500_USB,
					AB8500_USB_PHY_CTRL_REG,
					bit,
					bit);
		udelay(AB8500_V20_31952_DISABLE_DELAY_US);
	}
}

static void ab8500_usb_phy_disable(struct ab8500_usb *ab, bool sel_host)
{
	u8 bit;
	bit = sel_host ? AB8500_BIT_PHY_CTRL_HOST_EN :
			AB8500_BIT_PHY_CTRL_DEVICE_EN;

	ab8500_usb_wd_linkstatus(ab,bit);

	abx500_mask_and_set_register_interruptible(ab->dev,
				AB8500_USB,
				AB8500_USB_PHY_CTRL_REG,
				bit,
				0);

	/* Needed to disable the phy.*/
	ab8500_usb_wd_workaround(ab);

	clk_disable(ab->sysclk);

	ab8500_usb_regulator_ctrl(ab, sel_host, false);

	prcmu_qos_update_requirement(PRCMU_QOS_APE_OPP,
				(char *)dev_name(ab->dev), 50);

	if (!sel_host) {

		cancel_delayed_work_sync(&ab->work_usb_workaround);
		prcmu_qos_update_requirement(PRCMU_QOS_ARM_OPP,
			"usb", 25);
	}

	wake_unlock(&ab8500_musb_wakelock);
}

#define ab8500_usb_host_phy_en(ab)	ab8500_usb_phy_enable(ab, true)
#define ab8500_usb_host_phy_dis(ab)	ab8500_usb_phy_disable(ab, true)
#define ab8500_usb_peri_phy_en(ab)	ab8500_usb_phy_enable(ab, false)
#define ab8500_usb_peri_phy_dis(ab)	ab8500_usb_phy_disable(ab, false)

static int ab8505_usb_link_status_update(struct ab8500_usb *ab,
				enum ab8505_usb_link_status lsts)
{
	enum usb_phy_events event=0;

	dev_dbg(ab->dev, "ab8505_usb_link_status_update %d\n", lsts);

	switch (lsts) {
	case USB_LINK_ACA_RID_B_8505:
		event = USB_EVENT_RIDB;
	case USB_LINK_NOT_CONFIGURED_8505:
	case USB_LINK_RESERVED0_8505:
	case USB_LINK_RESERVED1_8505:
	case USB_LINK_RESERVED2_8505:
	case USB_LINK_RESERVED3_8505:
		if (ab->mode == USB_PERIPHERAL)
			atomic_notifier_call_chain(&ab->phy.notifier,
						USB_EVENT_CLEAN,
						&ab->vbus_draw);
		ab->mode = USB_IDLE;
		ab->phy.otg->default_a = false;
		ab->vbus_draw = 0;
		if (event != USB_EVENT_RIDB)
			event = USB_EVENT_NONE;
		break;

	case USB_LINK_ACA_RID_C_NM_8505:
		event = USB_EVENT_RIDC;
	case USB_LINK_STD_HOST_NC_8505:
	case USB_LINK_STD_HOST_C_NS_8505:
	case USB_LINK_STD_HOST_C_S_8505:
		if (ab->mode == USB_HOST) {
			ab->mode = USB_PERIPHERAL;
			ab8500_usb_host_phy_dis(ab);
			atomic_notifier_call_chain(&ab->phy.notifier,
							USB_EVENT_CLEAN,
							&ab->vbus_draw);
			ab8500_usb_peri_phy_en(ab);
		}
		if (ab->mode == USB_IDLE) {
			ab->mode = USB_PERIPHERAL;
			atomic_notifier_call_chain(&ab->phy.notifier,
						   USB_EVENT_PREPARE,
						   &ab->vbus_draw);
			ab8500_usb_peri_phy_en(ab);
		}
		if (event != USB_EVENT_RIDC)
			event = USB_EVENT_VBUS;
		break;
	case USB_LINK_ACA_RID_A_8505:
		event = USB_EVENT_RIDA;
	case USB_LINK_HM_IDGND_8505:
		if (ab->mode == USB_PERIPHERAL) {
			ab->mode = USB_HOST;
			ab8500_usb_peri_phy_dis(ab);
			atomic_notifier_call_chain(&ab->phy.notifier,
						   USB_EVENT_PREPARE,
						   &ab->vbus_draw);
			ab8500_usb_host_phy_en(ab);
		}
		if (ab->mode == USB_IDLE) {
			ab->mode = USB_HOST;
			atomic_notifier_call_chain(&ab->phy.notifier,
						   USB_EVENT_PREPARE,
						   &ab->vbus_draw);
			ab8500_usb_host_phy_en(ab);
		}
		ab->phy.otg->default_a = true;
		if (event != USB_EVENT_RIDA)
			event = USB_EVENT_ID;
			atomic_notifier_call_chain(&ab->phy.notifier,
						event,
						&ab->vbus_draw);
		break;

	case USB_LINK_DEDICATED_CHG_8505:
			ab->mode = USB_DEDICATED_CHG;
			event = USB_EVENT_CHARGER;
			atomic_notifier_call_chain(&ab->phy.notifier,
					event,
					&ab->vbus_draw);
		break;

	default:
		break;
	}
	return 0;
}

static int ab8500_usb_link_status_update(struct ab8500_usb *ab,
				enum ab8500_usb_link_status lsts)
{
	enum usb_phy_events event=0;

	dev_dbg(ab->dev, "ab8500_usb_link_status_update %d\n", lsts);

	switch (lsts) {
	case USB_LINK_ACA_RID_B_8500:
		event = USB_EVENT_RIDB;
	case USB_LINK_NOT_CONFIGURED_8500:
	case USB_LINK_RESERVED_8500:
	case USB_LINK_NOT_VALID_LINK_8500:
		if (ab->mode == USB_PERIPHERAL)
			atomic_notifier_call_chain(&ab->phy.notifier,
					   USB_EVENT_CLEAN,
					   &ab->vbus_draw);
		ab->mode = USB_IDLE;
		ab->phy.otg->default_a = false;
		ab->vbus_draw = 0;
		if (event != USB_EVENT_RIDB)
			event = USB_EVENT_NONE;
		break;
	case USB_LINK_ACA_RID_C_NM_8500:
	case USB_LINK_ACA_RID_C_HS_8500:
	case USB_LINK_ACA_RID_C_HS_CHIRP_8500:
		event = USB_EVENT_RIDC;
	case USB_LINK_STD_HOST_NC_8500:
	case USB_LINK_STD_HOST_C_NS_8500:
	case USB_LINK_STD_HOST_C_S_8500:
	case USB_LINK_HOST_CHG_NM_8500:
	case USB_LINK_HOST_CHG_HS_8500:
	case USB_LINK_HOST_CHG_HS_CHIRP_8500:
		if (ab->mode == USB_HOST) {
			ab->mode = USB_PERIPHERAL;
			ab8500_usb_host_phy_dis(ab);
			atomic_notifier_call_chain(&ab->phy.notifier,
						   USB_EVENT_PREPARE,
						   &ab->vbus_draw);
			ab8500_usb_peri_phy_en(ab);
		}
		if (ab->mode == USB_IDLE) {
			ab->mode = USB_PERIPHERAL;
			atomic_notifier_call_chain(&ab->phy.notifier,
						   USB_EVENT_PREPARE,
						   &ab->vbus_draw);
			ab8500_usb_peri_phy_en(ab);
		}
		if (event != USB_EVENT_RIDC)
			event = USB_EVENT_VBUS;
		break;

	case USB_LINK_ACA_RID_A_8500:
		event = USB_EVENT_RIDA;
	case USB_LINK_HM_IDGND_8500:
		if (ab->mode == USB_PERIPHERAL) {
			ab->mode = USB_HOST;
			ab8500_usb_peri_phy_dis(ab);
			atomic_notifier_call_chain(&ab->phy.notifier,
							   USB_EVENT_PREPARE,
						   &ab->vbus_draw);
			ab8500_usb_host_phy_en(ab);
		}
		if (ab->mode == USB_IDLE) {
			ab->mode = USB_HOST;
			atomic_notifier_call_chain(&ab->phy.notifier,
							   USB_EVENT_PREPARE,
						   &ab->vbus_draw);
			ab8500_usb_host_phy_en(ab);
		}
		ab->phy.otg->default_a = true;
		if (event != USB_EVENT_RIDA)
			event = USB_EVENT_ID;
		atomic_notifier_call_chain(&ab->phy.notifier,
				event,
				&ab->vbus_draw);
		break;

	case USB_LINK_DEDICATED_CHG_8500:
		ab->mode = USB_DEDICATED_CHG;
		event = USB_EVENT_CHARGER;
		atomic_notifier_call_chain(&ab->phy.notifier,
				event,
				&ab->vbus_draw);
		break;
	}
	return 0;
}

static int abx500_usb_link_status_update(struct ab8500_usb *ab)
{
	u8 reg;
	int ret = 0;

	if (!(ab->sysfs_flag)) {
		if (is_ab8500(ab->ab8500)) {
			enum ab8500_usb_link_status lsts;

			abx500_get_register_interruptible(ab->dev,
				AB8500_USB,
				AB8500_USB_LINE_STAT_REG,
				&reg);
			lsts = (reg >> 3) & 0x0F;
			ret = ab8500_usb_link_status_update(ab, lsts);
		}
		if (is_ab8505(ab->ab8500)) {
			enum ab8505_usb_link_status lsts;

			abx500_get_register_interruptible(ab->dev,
				AB8500_USB,
				AB8505_USB_LINE_STAT_REG,
				&reg);
			lsts = (reg >> 3) & 0x1F;
			ret = ab8505_usb_link_status_update(ab, lsts);
		}
	}
	return ret;
}

static void ab8500_usb_delayed_work(struct work_struct *work)
{
	struct delayed_work *dwork = to_delayed_work(work);
	struct ab8500_usb *ab = container_of(dwork, struct ab8500_usb, dwork);
	abx500_usb_link_status_update(ab);
}

static irqreturn_t ab8500_usb_disconnect_irq(int irq, void *data)
{
	struct ab8500_usb *ab = (struct ab8500_usb *) data;
	enum usb_phy_events event = USB_EVENT_NONE;

	/* Link status will not be updated till phy is disabled. */
	if (ab->mode == USB_HOST) {
		ab->phy.otg->default_a = false;
		ab->vbus_draw = 0;
		atomic_notifier_call_chain(&ab->phy.notifier,
					event, &ab->vbus_draw);
		ab8500_usb_host_phy_dis(ab);
	}
	if (ab->mode == USB_PERIPHERAL) {
		atomic_notifier_call_chain(&ab->phy.notifier,
				event, &ab->vbus_draw);
		ab8500_usb_peri_phy_dis(ab);
	}
	if (is_ab8500_2p0(ab->ab8500)) {
		if (ab->mode == USB_DEDICATED_CHG) {
			ab8500_usb_wd_linkstatus(ab, AB8500_BIT_PHY_CTRL_DEVICE_EN);
			abx500_mask_and_set_register_interruptible(ab->dev,
				AB8500_USB,
				AB8500_USB_PHY_CTRL_REG,
				AB8500_BIT_PHY_CTRL_DEVICE_EN,
				0);
		}
	}

	return IRQ_HANDLED;
}

static irqreturn_t ab8500_usb_v20_link_status_irq(int irq, void *data)
{
	struct ab8500_usb *ab = (struct ab8500_usb *) data;

	abx500_usb_link_status_update(ab);

	return IRQ_HANDLED;
}

static void ab8500_usb_phy_disable_work(struct work_struct *work)
{
	struct ab8500_usb *ab = container_of(work, struct ab8500_usb,
						phy_dis_work);

	if (!ab->phy.otg->host)
		ab8500_usb_host_phy_dis(ab);

	if (!ab->phy.otg->gadget)
		ab8500_usb_peri_phy_dis(ab);

}

static unsigned ab8500_eyediagram_workaroud(struct ab8500_usb *ab, unsigned mA)
{
	/* AB V2 has eye diagram issues when drawing more
	 * than 100mA from VBUS.So setting charging current
	 * to 100mA in case of standard host
	 */
	if (is_ab8500_2p0_or_earlier(ab->ab8500))
		if (mA > 100)
			mA = 100;

	return mA;
}

#ifdef CONFIG_USB_OTG_20
static int ab8500_usb_start_srp(struct usb_phy *phy, unsigned mA)
{
	struct ab8500_usb *ab;

	if (!phy)
		return -ENODEV;

	ab = phy_to_ab(phy);

	atomic_notifier_call_chain(&ab->phy.notifier,
				   USB_EVENT_PREPARE,
				   &ab->vbus_draw);

	ab8500_usb_peri_phy_en(ab);

	return 0;
}
#endif

static int ab8500_usb_set_power(struct usb_phy *phy, unsigned mA)
{
	struct ab8500_usb *ab;

	if (!phy)
		return -ENODEV;

	ab = phy_to_ab(phy);

	mA = ab8500_eyediagram_workaroud(ab, mA);

	ab->vbus_draw = mA;

	atomic_notifier_call_chain(&ab->phy.notifier,
				USB_EVENT_VBUS, &ab->vbus_draw);
	return 0;
}

static int ab8500_usb_set_suspend(struct usb_phy *x, int suspend)
{
	/* TODO */
	return 0;
}

static int ab8500_usb_set_peripheral(struct usb_otg *otg,
					struct usb_gadget *gadget)
{
	struct ab8500_usb *ab;

	if (!otg)
		return -ENODEV;

	ab = phy_to_ab(otg->phy);

	ab->phy.otg->gadget = gadget;
	/* Some drivers call this function in atomic context.
	 * Do not update ab8500 registers directly till this
	 * is fixed.
	 */
	if (!gadget && ab->mode == USB_PERIPHERAL)
		schedule_work(&ab->phy_dis_work);

	return 0;
}

static int ab8500_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
{
	struct ab8500_usb *ab;

	if (!otg)
		return -ENODEV;

	ab = phy_to_ab(otg->phy);

	ab->phy.otg->host = host;

	/* Some drivers call this function in atomic context.
	 * Do not update ab8500 registers directly till this
	 * is fixed.
	 */
	if (!host && ab->mode == USB_HOST)
		schedule_work(&ab->phy_dis_work);

	return 0;
}
/**
 * ab8500_usb_boot_detect : detect the USB cable during boot time.
 * @device: value for device.
 *
 * This function is used to detect the USB cable during boot time.
 */
static int ab8500_usb_boot_detect(struct ab8500_usb *ab)
{
	/* Disabling PHY before selective enable or disable */
	abx500_mask_and_set_register_interruptible(ab->dev,
				AB8500_USB,
				AB8500_USB_PHY_CTRL_REG,
				AB8500_BIT_PHY_CTRL_DEVICE_EN,
				AB8500_BIT_PHY_CTRL_DEVICE_EN);

	udelay(100);

	abx500_mask_and_set_register_interruptible(ab->dev,
				AB8500_USB,
				AB8500_USB_PHY_CTRL_REG,
				AB8500_BIT_PHY_CTRL_DEVICE_EN,
				0);

	abx500_mask_and_set_register_interruptible(ab->dev,
				AB8500_USB,
				AB8500_USB_PHY_CTRL_REG,
				AB8500_BIT_PHY_CTRL_HOST_EN,
				AB8500_BIT_PHY_CTRL_HOST_EN);

	udelay(100);

	abx500_mask_and_set_register_interruptible(ab->dev,
				AB8500_USB,
				AB8500_USB_PHY_CTRL_REG,
				AB8500_BIT_PHY_CTRL_HOST_EN,
				0);

	return 0;
}

static void ab8500_usb_regulator_put(struct ab8500_usb *ab)
{

	if (ab->v_ape)
		regulator_put(ab->v_ape);

	if (ab->v_ulpi)
		regulator_put(ab->v_ulpi);

	if (ab->v_musb)
		regulator_put(ab->v_musb);
}

static int ab8500_usb_regulator_get(struct ab8500_usb *ab)
{
	int err;

	ab->v_ape = regulator_get(ab->dev, "v-ape");
	if (IS_ERR(ab->v_ape)) {
		dev_err(ab->dev, "Could not get v-ape supply\n");
		err = PTR_ERR(ab->v_ape);
		return err;
	}

	ab->v_ulpi = regulator_get(ab->dev, "vddulpivio18");
	if (IS_ERR(ab->v_ulpi)) {
		dev_err(ab->dev, "Could not get vddulpivio18 supply\n");
		err = PTR_ERR(ab->v_ulpi);
		return err;
	}

	ab->v_musb = regulator_get(ab->dev, "musb_1v8");
	if (IS_ERR(ab->v_musb)) {
		dev_err(ab->dev, "Could not get musb_1v8 supply\n");
		err = PTR_ERR(ab->v_musb);
		return err;
	}

	return 0;
}

static void ab8500_usb_irq_free(struct ab8500_usb *ab)
{
	if (ab->irq_num_id_rise)
		free_irq(ab->irq_num_id_rise, ab);

	if (ab->irq_num_id_fall)
		free_irq(ab->irq_num_id_fall, ab);

	if (ab->irq_num_vbus_rise)
		free_irq(ab->irq_num_vbus_rise, ab);

	if (ab->irq_num_vbus_fall)
		free_irq(ab->irq_num_vbus_fall, ab);

	if (ab->irq_num_link_status)
		free_irq(ab->irq_num_link_status, ab);
}

static int ab8500_usb_irq_setup(struct platform_device *pdev,
				struct ab8500_usb *ab)
{
	int err;
	int irq;

	if (!is_ab8500_1p0_or_earlier(ab->ab8500)) {
		irq = platform_get_irq_byname(pdev, "USB_LINK_STATUS");
		if (irq < 0) {
			err = irq;
			dev_err(&pdev->dev, "Link status irq not found\n");
			goto irq_fail;
		}

		err = request_threaded_irq(irq, NULL,
			ab8500_usb_v20_link_status_irq,
			IRQF_NO_SUSPEND | IRQF_SHARED,
			"usb-link-status", ab);
		if (err < 0) {
			dev_err(ab->dev,
				"request_irq failed for link status irq\n");
			return err;
		}
		ab->irq_num_link_status = irq;
	}

	irq = platform_get_irq_byname(pdev, "ID_WAKEUP_F");
	if (irq < 0) {
		err = irq;
		dev_err(&pdev->dev, "ID fall irq not found\n");
		return ab->irq_num_id_fall;
	}
	err = request_threaded_irq(irq, NULL,
		ab8500_usb_disconnect_irq,
		IRQF_NO_SUSPEND | IRQF_SHARED,
		"usb-id-fall", ab);
	if (err < 0) {
		dev_err(ab->dev, "request_irq failed for ID fall irq\n");
		goto irq_fail;
	}
	ab->irq_num_id_fall = irq;

	irq = platform_get_irq_byname(pdev, "VBUS_DET_F");
	if (irq < 0) {
		err = irq;
		dev_err(&pdev->dev, "VBUS fall irq not found\n");
		goto irq_fail;
	}
	err = request_threaded_irq(irq, NULL,
		ab8500_usb_disconnect_irq,
		IRQF_NO_SUSPEND | IRQF_SHARED,
		"usb-vbus-fall", ab);
	if (err < 0) {
		dev_err(ab->dev, "request_irq failed for Vbus fall irq\n");
		goto irq_fail;
	}
	ab->irq_num_vbus_fall = irq;

	return 0;

irq_fail:
	ab8500_usb_irq_free(ab);
	return err;
}

/* Sys interfaces */
static ssize_t
serial_number_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	u32 bufer[5];
	void __iomem *backup_ram = NULL;

	backup_ram = ioremap(PUBLIC_ID_BACKUPRAM1, 0x14);

	if (backup_ram) {
		bufer[0] = readl(backup_ram);
		bufer[1] = readl(backup_ram + 4);
		bufer[2] = readl(backup_ram + 8);
		bufer[3] = readl(backup_ram + 0x0c);
		bufer[4] = readl(backup_ram + 0x10);

		snprintf(buf, MAX_USB_SERIAL_NUMBER_LEN+1,
				"%.8X%.8X%.8X%.8X%.8X",
			bufer[0], bufer[1], bufer[2], bufer[3], bufer[4]);

		iounmap(backup_ram);
	} else
			dev_err(dev, "$$\n");

	return strlen(buf);
}

static DEVICE_ATTR(serial_number, 0644, serial_number_show, NULL);

static ssize_t
boot_time_device_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct ab8500_usb *ab = dev_get_drvdata(dev);
	u8 val = ab->sysfs_flag;

	snprintf(buf, 2, "%d", val);

	return strlen(buf);
}

static ssize_t
boot_time_device_store(struct device *dev, struct device_attribute *attr,
		const char *buf, size_t n)
{
	struct ab8500_usb *ab = dev_get_drvdata(dev);

	ab->sysfs_flag = false;

	abx500_usb_link_status_update(ab);

	return n;
}
static DEVICE_ATTR(boot_time_device, 0644,
			boot_time_device_show, boot_time_device_store);


static struct attribute *ab8500_usb_attributes[] = {
	&dev_attr_serial_number.attr,
	&dev_attr_boot_time_device.attr,
	NULL
};
static const struct attribute_group ab8500_attr_group = {
	.attrs = ab8500_usb_attributes,
};

static int ab8500_create_sysfsentries(struct ab8500_usb *ab)
{
	int err;

	err = sysfs_create_group(&ab->dev->kobj, &ab8500_attr_group);
	if (err)
		sysfs_remove_group(&ab->dev->kobj, &ab8500_attr_group);

	return err;
}

static int __devinit ab8500_usb_probe(struct platform_device *pdev)
{
	struct ab8500_usb	*ab;
	struct ab8500 *ab8500;
	struct usb_otg		*otg;
	int err;
	int rev;
	int ret = -1;

	ab8500 = dev_get_drvdata(pdev->dev.parent);
	rev = abx500_get_chip_id(&pdev->dev);

	if (is_ab8500_1p1_or_earlier(ab8500)) {
		dev_err(&pdev->dev, "Unsupported AB8500 chip rev=%d\n", rev);
		return -ENODEV;
	}

	ab = kzalloc(sizeof *ab, GFP_KERNEL);
	if (!ab)
		return -ENOMEM;

	otg = kzalloc(sizeof *otg, GFP_KERNEL);
	if (!otg) {
		kfree(ab);
		return -ENOMEM;
	}

	ab->dev			= &pdev->dev;
	ab->ab8500		= ab8500;
	ab->phy.dev		= ab->dev;
	ab->phy.otg		= otg;
	ab->phy.label		= "ab8500";
	ab->phy.set_suspend	= ab8500_usb_set_suspend;
	ab->phy.set_power	= ab8500_usb_set_power;
	ab->phy.state		= OTG_STATE_B_IDLE;

	otg->phy		= &ab->phy;
	otg->set_host		= ab8500_usb_set_host;
	otg->set_peripheral	= ab8500_usb_set_peripheral;
#ifdef CONFIG_USB_OTG_20
	ab->otg.start_srp	= ab8500_usb_start_srp;
#endif
	ab->sysfs_flag		= true;

	platform_set_drvdata(pdev, ab);
	dev_set_drvdata(ab->dev, ab);
	ATOMIC_INIT_NOTIFIER_HEAD(&ab->phy.notifier);

	/* v1: Wait for link status to become stable.
	 * all: Updates form set_host and set_peripheral as they are atomic.
	 */
	INIT_DELAYED_WORK(&ab->dwork, ab8500_usb_delayed_work);

	/* all: Disable phy when called from set_host and set_peripheral */
	INIT_WORK(&ab->phy_dis_work, ab8500_usb_phy_disable_work);

	INIT_DELAYED_WORK_DEFERRABLE(&ab->work_usb_workaround,
							ab8500_usb_load);
	err = ab8500_usb_regulator_get(ab);
	if (err)
		goto fail0;

	ab->sysclk = clk_get(ab->dev, "sysclk");
	if (IS_ERR(ab->sysclk)) {
		err = PTR_ERR(ab->sysclk);
		goto fail1;
	}

	err = ab8500_usb_irq_setup(pdev, ab);
	if (err < 0)
		goto fail2;

	err = usb_set_transceiver(&ab->phy);
	if (err) {
		dev_err(&pdev->dev, "Can't register transceiver\n");
		goto fail3;
	}

	/* Write Phy tuning values */
	if (!is_ab8500_2p0_or_earlier(ab->ab8500)) {
		/* Enable the PBT/Bank 0x12 access */
		ret = abx500_set_register_interruptible(ab->dev,
							AB8500_DEVELOPMENT,
							AB8500_BANK12_ACCESS,
							0x01);
		if (ret < 0)
			printk(KERN_ERR "Failed to enable bank12"
						" access ret=%d\n", ret);

			ret = abx500_set_register_interruptible(ab->dev,
							AB8500_DEBUG,
							AB8500_USB_PHY_TUNE1,
							0xC8);
		if (ret < 0)
			printk(KERN_ERR "Failed to set PHY_TUNE1"
						" register ret=%d\n", ret);

			ret = abx500_set_register_interruptible(ab->dev,
							AB8500_DEBUG,
							AB8500_USB_PHY_TUNE2,
							0x00);
		if (ret < 0)
			printk(KERN_ERR "Failed to set PHY_TUNE2"
						" register ret=%d\n", ret);

			ret = abx500_set_register_interruptible(ab->dev,
							AB8500_DEBUG,
							AB8500_USB_PHY_TUNE3,
							0x78);

		if (ret < 0)
			printk(KERN_ERR "Failed to set PHY_TUNE3"
						" regester ret=%d\n", ret);

		/* Switch to normal mode/disable Bank 0x12 access */
			ret = abx500_set_register_interruptible(ab->dev,
							AB8500_DEVELOPMENT,
							AB8500_BANK12_ACCESS,
							0x00);

		if (ret < 0)
			printk(KERN_ERR "Failed to switch bank12"
						" access ret=%d\n", ret);
	}
	/* Needed to enable ID detection. */
	ab8500_usb_wd_workaround(ab);

	prcmu_qos_add_requirement(PRCMU_QOS_APE_OPP,
			(char *)dev_name(ab->dev), 50);
	dev_info(&pdev->dev, "revision 0x%2x driver initialized\n", rev);

	prcmu_qos_add_requirement(PRCMU_QOS_ARM_OPP, "usb", 25);
	wake_lock_init(&ab8500_musb_wakelock, WAKE_LOCK_SUSPEND, "ab8500-usb");

	err = ab8500_usb_boot_detect(ab);
	if (err < 0)
		goto fail3;

	err = ab8500_create_sysfsentries(ab);
	if (err)
		goto fail3;

	return 0;
fail3:
	ab8500_usb_irq_free(ab);
fail2:
	clk_put(ab->sysclk);
fail1:
	ab8500_usb_regulator_put(ab);
fail0:
	kfree(otg);
	kfree(ab);
	return err;
}

static int __devexit ab8500_usb_remove(struct platform_device *pdev)
{
	struct ab8500_usb *ab = platform_get_drvdata(pdev);

	ab8500_usb_irq_free(ab);

	cancel_delayed_work_sync(&ab->dwork);

	cancel_work_sync(&ab->phy_dis_work);

	usb_set_transceiver(NULL);

	if (ab->mode == USB_HOST)
		ab8500_usb_host_phy_dis(ab);
	else if (ab->mode == USB_PERIPHERAL)
		ab8500_usb_peri_phy_dis(ab);

	clk_put(ab->sysclk);

	ab8500_usb_regulator_put(ab);

	platform_set_drvdata(pdev, NULL);

	kfree(ab->phy.otg);
	kfree(ab);

	return 0;
}

static struct platform_driver ab8500_usb_driver = {
	.probe		= ab8500_usb_probe,
	.remove		= __devexit_p(ab8500_usb_remove),
	.driver		= {
		.name	= "ab8500-usb",
		.owner	= THIS_MODULE,
	},
};

static int __init ab8500_usb_init(void)
{
	return platform_driver_register(&ab8500_usb_driver);
}
subsys_initcall(ab8500_usb_init);

static void __exit ab8500_usb_exit(void)
{
	platform_driver_unregister(&ab8500_usb_driver);
}
module_exit(ab8500_usb_exit);

MODULE_ALIAS("platform:ab8500_usb");
MODULE_AUTHOR("ST-Ericsson AB");
MODULE_DESCRIPTION("AB8500 usb transceiver driver");
MODULE_LICENSE("GPL");