summaryrefslogtreecommitdiff
path: root/drivers/power/max77660-charger-extcon.c
blob: 518c56deaa6a86f63c6f85014a1cd9bbbca1b6b1 (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
/*
 * max77660-charger-extcon.c -- MAXIM MAX77660 VBUS detection.
 *
 * Copyright (c) 2013, NVIDIA Corporation.
 *
 * Author: Syed Rafiuddin <srafiuddin@nvidia.com>
 * Author: Laxman Dewangan <ldewangan@nvidia.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 version 2.
 *
 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
 * whether express or implied; 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., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307, USA
 */

#include <linux/module.h>
#include <linux/err.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/pm.h>
#include <linux/extcon.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/mfd/max77660/max77660-core.h>
#include <linux/workqueue.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/time.h>
#include <linux/timer.h>
#include <linux/power_supply.h>

static int max77660_chrg_wdt[] = {16, 32, 64, 128};

struct max77660_charger {
	struct max77660_charger_platform_data *pdata;
	struct device *dev;
	struct power_supply usb_psy;
	struct power_supply dc_psy;
	int irq;
	int usb_online;
	int ac_online;
	int is_100ma;
	int status;
	int in_current_lim;
	void (*update_status)(int, int);
	int wdt_timeout;
};

struct max77660_chg_extcon {
	struct device			*dev;
	struct device			*parent;
	struct extcon_dev		*edev;
	struct regulator_dev		*chg_rdev;
	struct regulator_dev		*rdev;
	struct max77660_charger		*charger;
	int				chg_irq;
	int				chg_wdt_irq;
	struct regulator_desc		chg_reg_desc;
	struct regulator_init_data	chg_reg_init_data;
};

struct max77660_chg_extcon *max77660_ext;

const char *max77660_excon_cable[] = {
	[0] = "USB",
	NULL,
};

static inline int fchg_current(int x)
{
	if (x >= 1620)
		return (x+60)/60;
	else if (x >= 250)
		return (x-200)/50;
	else
		return 0;
}

static int max77660_battery_detect(struct max77660_chg_extcon *chip)
{
	int ret = 0;
	u8 status;

	ret = max77660_reg_read(chip->parent, MAX77660_CHG_SLAVE,
			MAX77660_CHARGER_DETAILS1, &status);
	if (ret < 0) {
		dev_err(chip->dev, "CHARGER_CHGINT read failed: %d\n", ret);
		return ret;
	}
	if ((status & MAX77660_BATDET_DTLS) == MAX77660_BATDET_DTLS_NO_BAT)
		return -EPERM;
	return 0;
}

static int max77660_charger_init(struct max77660_chg_extcon *chip, int enable)
{
	struct max77660_charger *charger = chip->charger;
	u8 reg_val = 0;
	int ret;
	u8 read_val;

	/* Enable USB suspend if 2mA is current configuration */
	if (charger->in_current_lim == 2)
		ret = max77660_reg_set_bits(chip->parent,
				MAX77660_CHG_SLAVE,
				MAX77660_CHARGER_USBCHGCTRL,
				MAX77660_USBCHGCTRL_USB_SUSPEND);
	else
		ret = max77660_reg_clr_bits(chip->parent,
				MAX77660_CHG_SLAVE,
				MAX77660_CHARGER_USBCHGCTRL,
				MAX77660_USBCHGCTRL_USB_SUSPEND);
	if (ret < 0)
		return ret;

	charger->in_current_lim = fchg_current(charger->in_current_lim);

	/* unlock charger protection */
	reg_val = MAX77660_CHGPROT_UNLOCKED<<MAX77660_CHGPROT_SHIFT;
	ret = max77660_reg_write(chip->parent, MAX77660_CHG_SLAVE,
		MAX77660_CHARGER_CHGCTRL1,
		MAX77660_CHARGER_CHGPROT_MASK |
		MAX77660_CHARGER_BUCK_EN_MASK |
		MAX77660_CHARGER_JEITA_EN_MASK);
	if (ret < 0)
		return ret;

	if (enable) {
		/* enable charger */
		/* Set DCILMT to 1A */
		ret = max77660_reg_write(chip->parent,
				MAX77660_CHG_SLAVE,
				MAX77660_CHARGER_DCCRNT,
				MAX77660_DCLIMIT_1A |
				MAX77660_DC_WC_CNTL_DC);
		if (ret < 0)
			return ret;

		/* Fast charge to 5 hours, fast charge current to 1.1A */
		ret = max77660_reg_update(chip->parent,
				MAX77660_CHG_SLAVE, MAX77660_CHARGER_FCHGCRNT,
				chip->charger->in_current_lim,
				MAX77660_CHGCC_MASK);
		if (ret < 0)
			return ret;

		ret = max77660_reg_read(chip->parent,
				MAX77660_CHG_SLAVE,
				MAX77660_CHARGER_FCHGCRNT, &read_val);
		if (ret < 0)
			return ret;

		/* Set TOPOFF to 10 min */
		ret = max77660_reg_write(chip->parent,
				MAX77660_CHG_SLAVE,
				MAX77660_CHARGER_TOPOFF,
				MAX77660_ITOPOFF_200MA |
				MAX77660_TOPOFFT_10MIN);
		if (ret < 0)
			return ret;
		/* MBATREG to 4.2V */
		ret = max77660_reg_write(chip->parent,
				MAX77660_CHG_SLAVE,
				MAX77660_CHARGER_BATREGCTRL,
				MAX77660_MBATREG_4200MV);
		if (ret < 0)
			return ret;
		/* MBATREGMAX to 4.2V */
		ret = max77660_reg_write(chip->parent,
				MAX77660_CHG_SLAVE,
				MAX77660_CHARGER_MBATREGMAX,
				MAX77660_MBATREG_4200MV);
		if (ret < 0)
			return ret;

		/* DSILIM_EN = 1; CEN= 1; QBATEN = 0; VSYSREG = 3.6V */
		ret = max77660_reg_write(chip->parent,
				MAX77660_CHG_SLAVE,
				MAX77660_CHARGER_CHGCTRL2,
				MAX77660_VSYSREG_3600MV |
				MAX77660_CEN_MASK |
				MAX77660_PREQ_CUR_MASK |
				MAX77660_DCILIM_EN_MASK);
		if (ret < 0)
			return ret;
		/* Enable top level charging */
		ret = max77660_reg_set_bits(chip->parent, MAX77660_PWR_SLAVE,
			MAX77660_REG_GLOBAL_CFG1, MAX77660_GLBLCNFG1_ENCHGTL);
		if (ret < 0)
			return ret;

	} else {
		/* disable charge */
		/* Clear top level charge */
		ret = max77660_reg_set_bits(chip->parent, MAX77660_PWR_SLAVE,
			MAX77660_REG_GLOBAL_CFG1, MAX77660_GLBLCNFG1_ENCHGTL);
		if (ret < 0)
			return ret;
		/* Clear CEN */
		ret = max77660_reg_clr_bits(chip->parent,
			MAX77660_CHG_SLAVE, MAX77660_CHARGER_CHGCTRL2,
			MAX77660_CEN_MASK);
		if (ret < 0)
			return ret;
	}
	dev_info(chip->dev, "%s\n", (enable) ? "Enable charger" :
			"Disable charger");
	return 0;
}

static int max77660_set_charging_current(struct regulator_dev *rdev,
		int min_uA, int max_uA)
{
	struct max77660_chg_extcon *chip = rdev_get_drvdata(rdev);
	struct max77660_charger *charger = chip->charger;
	int ret;
	u8 status;

	ret = max77660_battery_detect(chip);
	if (ret < 0) {
		dev_err(chip->dev,
			"Battery detection failed\n");
		goto error;
	}

	charger->in_current_lim = max_uA/1000;
	charger->status = 0;

	ret = max77660_reg_read(chip->parent, MAX77660_CHG_SLAVE,
			MAX77660_CHARGER_CHGSTAT, &status);
	if (ret < 0) {
		dev_err(chip->dev, "CHSTAT read failed: %d\n", ret);
		return ret;
	}
	if (charger->in_current_lim == 0 &&
			!(status & MAX77660_CHG_CHGINT_DC_UVP)) {
		charger->is_100ma = 0;
		return 0;
	}

	if (charger->in_current_lim == 0) {
		charger->is_100ma = 0;
		charger->usb_online = 0;
		charger->ac_online = 0;
		ret = max77660_charger_init(chip, false);
		if (ret < 0)
			goto error;
		charger->status = 2;
		if (charger->update_status)
			charger->update_status
				(charger->status, 2);
	} else {
		if (charger->in_current_lim == 100) {
			charger->is_100ma = 1;
			charger->usb_online = 1;
			charger->ac_online = 0;
		}

		charger->status = 1;
		if (charger->in_current_lim == 500) {
			if (!charger->is_100ma) {
				charger->ac_online = 1;
				charger->usb_online = 0;
			}
		} else if (charger->in_current_lim > 500) {
			charger->ac_online = 1;
			charger->usb_online = 0;
		} else if (charger->in_current_lim == 2) {
			charger->ac_online = 0;
			charger->usb_online = 1;
		}

		ret = max77660_charger_init(chip, true);
		if (ret < 0)
			goto error;

		if (charger->update_status)
			charger->update_status
				(charger->status, 1);
	}

	power_supply_changed(&charger->usb_psy);
	power_supply_changed(&charger->dc_psy);
	return 0;
error:
	return ret;
}

static struct regulator_ops max77660_charger_ops = {
	.set_current_limit = max77660_set_charging_current,
};

static int max77660_init_charger_regulator(struct max77660_chg_extcon *chip,
	struct max77660_charger_platform_data *pdata)
{
	struct regulator_config config = { };
	int ret = 0;

	if (!pdata) {
		dev_err(chip->dev, "No charger platform data\n");
		return 0;
	}

	chip->chg_reg_desc.name  = "max77660-charger";
	chip->chg_reg_desc.ops   = &max77660_charger_ops;
	chip->chg_reg_desc.type  = REGULATOR_CURRENT;
	chip->chg_reg_desc.owner = THIS_MODULE;

	chip->chg_reg_init_data.supply_regulator     = NULL;
	chip->chg_reg_init_data.regulator_init	= NULL;
	chip->chg_reg_init_data.num_consumer_supplies =
				pdata->num_consumer_supplies;
	chip->chg_reg_init_data.consumer_supplies    =
				pdata->consumer_supplies;
	chip->chg_reg_init_data.driver_data	   = chip->charger;
	chip->chg_reg_init_data.constraints.name     = "max77660-charger";
	chip->chg_reg_init_data.constraints.min_uA   = 0;
	chip->chg_reg_init_data.constraints.max_uA   =
			pdata->max_charge_current_mA * 1000;

	 chip->chg_reg_init_data.constraints.valid_modes_mask =
						REGULATOR_MODE_NORMAL |
						REGULATOR_MODE_STANDBY;

	chip->chg_reg_init_data.constraints.valid_ops_mask =
						REGULATOR_CHANGE_MODE |
						REGULATOR_CHANGE_STATUS |
						REGULATOR_CHANGE_CURRENT;

	config.dev = chip->dev;
	config.init_data = &chip->chg_reg_init_data;
	config.driver_data = chip;

	chip->chg_rdev = regulator_register(&chip->chg_reg_desc, &config);
	if (IS_ERR(chip->chg_rdev)) {
		ret = PTR_ERR(chip->chg_rdev);
		dev_err(chip->dev,
			"vbus-charger regulator register failed %d\n", ret);
	}
	return ret;
}

static int max77660_usb_get_property(struct power_supply *psy,
		enum power_supply_property psp,
		union power_supply_propval *val)
{
	 struct max77660_charger *chip;

	 chip = container_of(psy, struct max77660_charger, usb_psy);

	 if (psp == POWER_SUPPLY_PROP_ONLINE)
		val->intval = chip->usb_online;
	 else
		return -EINVAL;
	 return 0;
}

static int max77660_ac_get_property(struct power_supply *psy,
	enum power_supply_property psp,
	union power_supply_propval *val)
{
	struct max77660_charger *chip;

	chip = container_of(psy, struct max77660_charger, dc_psy);

	if (psp == POWER_SUPPLY_PROP_ONLINE)
		val->intval = chip->ac_online;
	else
		return -EINVAL;
	return 0;
}

static int max77660_chg_extcon_cable_update(
		struct max77660_chg_extcon *chg_extcon)
{
	int ret;
	u8 status;

	ret = max77660_reg_read(chg_extcon->parent, MAX77660_CHG_SLAVE,
			MAX77660_CHARGER_CHGSTAT, &status);
	if (ret < 0) {
		dev_err(chg_extcon->dev, "CHSTAT read failed: %d\n", ret);
		return ret;
	}
	if (status & MAX77660_CHG_CHGINT_DC_UVP)
		extcon_set_cable_state(chg_extcon->edev, "USB", false);
	else
		extcon_set_cable_state(chg_extcon->edev, "USB", true);

	dev_info(chg_extcon->dev, "VBUS %s status: 0x%02x\n",
		(status & MAX77660_CHG_CHGINT_DC_UVP) ? "Invalid" : "Valid",
		status);

	return 0;
}

static irqreturn_t max77660_chg_extcon_irq(int irq, void *data)
{
	struct max77660_chg_extcon *chg_extcon = data;
	u8 status;
	int ret;

	ret = max77660_reg_read(chg_extcon->parent, MAX77660_CHG_SLAVE,
			MAX77660_CHARGER_CHGINT, &status);
	if (ret < 0) {
		dev_err(chg_extcon->dev, "CHGSTAT read failed: %d\n", ret);
		goto out;
	}

	if (status & MAX77660_CHG_CHGINT_DC_UVP)
		max77660_chg_extcon_cable_update(chg_extcon);
	else
		dev_err(chg_extcon->dev, "CHG-IRQ for unknown reason, 0x%02x\n",
			status);
out:
	return IRQ_HANDLED;
}

static int max77660_charger_wdt(struct max77660_chg_extcon *chip)
{
	struct max77660_charger *charger = chip->charger;
	int i, ret = 0;

	for (i = 0; i < ARRAY_SIZE(max77660_chrg_wdt); ++i) {
		if (max77660_chrg_wdt[i] >= charger->wdt_timeout)
			break;
	}

	if (i == ARRAY_SIZE(max77660_chrg_wdt)) {
		dev_err(chip->dev, "Charger WDT %d sec is not supported\n",
			charger->wdt_timeout);
		return -EINVAL;
	}

	ret = max77660_reg_update(chip->parent, MAX77660_PWR_SLAVE,
			  MAX77660_REG_GLOBAL_CFG2,
			  MAX77660_GLBLCNFG2_TWD_CHG_MASK,
			  MAX77660_GLBLCNFG2_TWD_CHG(i));
	if (ret < 0) {
		dev_err(chip->dev,
			"GLOBAL_CFG2 update failed: %d\n", ret);
		return ret;
	}

	if (charger->wdt_timeout)
		ret = max77660_reg_set_bits(chip->parent, MAX77660_PWR_SLAVE,
			MAX77660_REG_GLOBAL_CFG1, MAX77660_GLBLCNFG1_ENCHGTL);
	else
		ret = max77660_reg_clr_bits(chip->parent, MAX77660_PWR_SLAVE,
			MAX77660_REG_GLOBAL_CFG1, MAX77660_GLBLCNFG1_ENCHGTL);
	if (ret < 0) {
		dev_err(chip->dev,
			"GLBLCNFG1_ENCHGTL update failed: %d\n", ret);
		 return ret;
	}
	return ret;
}

static irqreturn_t max77660_chg_wdt_irq(int irq, void *data)
{
	 struct max77660_chg_extcon *chip = (struct max77660_chg_extcon *)data;
	 int ret;

	ret = max77660_reg_write(chip->parent, MAX77660_PWR_SLAVE,
		MAX77660_REG_GLOBAL_CFG6,
		MAX77660_GLBLCNFG4_WDTC_SYS_CLR);
	if (ret < 0)
		dev_err(chip->dev, "GLOBAL_CFG4 update failed: %d\n", ret);

	 return IRQ_HANDLED;
}

static int max77660_vbus_enable_time(struct regulator_dev *rdev)
{
	 return 500000;
}

static int max77660_vbus_is_enabled(struct regulator_dev *rdev)
{
	struct max77660_chg_extcon *chg_extcon =rdev_get_drvdata(rdev);
	int ret;
	u8 val;

	ret = max77660_reg_read(chg_extcon->parent, MAX77660_CHG_SLAVE,
					MAX77660_CHARGER_RBOOST, &val);
	if (ret < 0) {
		dev_err(chg_extcon->dev, "RBOOST read failed: %d\n", ret);
		return ret;
	}
	return !!(val & MAX77660_RBOOST_RBOOSTEN);
}

static int max77660_vbus_enable(struct regulator_dev *rdev)
{
	struct max77660_chg_extcon *chg_extcon =rdev_get_drvdata(rdev);
	int ret;

	ret = max77660_reg_update(chg_extcon->parent, MAX77660_CHG_SLAVE,
			MAX77660_CHARGER_RBOOST,
			MAX77660_RBOOST_RBOUT_VOUT(0x6),
			MAX77660_RBOOST_RBOUT_MASK);
	if (ret < 0) {
		dev_err(chg_extcon->dev, "RBOOST update failed: %d\n", ret);
		return ret;
	}

	ret = max77660_reg_set_bits(chg_extcon->parent, MAX77660_CHG_SLAVE,
			MAX77660_CHARGER_RBOOST, MAX77660_RBOOST_RBOOSTEN);
	if (ret < 0)
		dev_err(chg_extcon->dev, "RBOOST setbits failed: %d\n", ret);
	return ret;
}

static int max77660_vbus_disable(struct regulator_dev *rdev)
{
	struct max77660_chg_extcon *chg_extcon =rdev_get_drvdata(rdev);
	 int ret;

	ret = max77660_reg_clr_bits(chg_extcon->parent, MAX77660_CHG_SLAVE,
			MAX77660_CHARGER_RBOOST, MAX77660_RBOOST_RBOOSTEN);
	if (ret < 0)
		dev_err(chg_extcon->dev, "RBOOST clrbits failed: %d\n", ret);
	 return ret;
}

static struct regulator_ops max77660_vbus_ops = {
	.enable		= max77660_vbus_enable,
	.disable	= max77660_vbus_disable,
	.is_enabled	= max77660_vbus_is_enabled,
	.enable_time	= max77660_vbus_enable_time,
};

static struct regulator_desc max77660_vbus_desc = {
	 .name = "max77660-vbus",
	 .ops = &max77660_vbus_ops,
	 .type = REGULATOR_VOLTAGE,
	 .owner = THIS_MODULE,
};

static enum power_supply_property charger_props[] = {
	POWER_SUPPLY_PROP_ONLINE,
};

static int max77660_psy_init(struct max77660_chg_extcon *chg_extcon)
{
	struct max77660_charger *charger = chg_extcon->charger;
	 int ret = 0;

	charger->dc_psy.name		= "ac";
	charger->dc_psy.type		= POWER_SUPPLY_TYPE_MAINS;
	charger->dc_psy.get_property	= max77660_ac_get_property;
	charger->dc_psy.properties	= charger_props;
	charger->dc_psy.num_properties	= ARRAY_SIZE(charger_props);

	ret = power_supply_register(chg_extcon->dev, &charger->dc_psy);
	if (ret < 0) {
		dev_err(chg_extcon->dev,
			"power_supply_register dc failed rc = %d\n", ret);
		 goto out;
	}

	charger->usb_psy.name		= "usb";
	charger->usb_psy.type		= POWER_SUPPLY_TYPE_USB;
	charger->usb_psy.get_property	= max77660_usb_get_property;
	charger->usb_psy.properties	= charger_props;
	charger->usb_psy.num_properties	= ARRAY_SIZE(charger_props);

	ret = power_supply_register(chg_extcon->dev, &charger->usb_psy);

	if (ret < 0) {
		dev_err(chg_extcon->dev,
			"power_supply_register usb failed rc = %d\n", ret);
		goto ac_psy_err;
	}

	return 0;

ac_psy_err:
	power_supply_unregister(&charger->dc_psy);
out:
	return ret;
}

static int max77660_chg_extcon_probe(struct platform_device *pdev)
{
	struct max77660_chg_extcon *chg_extcon;
	struct max77660_platform_data *pdata;
	struct max77660_charger_platform_data *chg_pdata;
	struct extcon_dev *edev;
	struct regulator_config config = { };
	struct max77660_charger *charger;
	int ret;

	pdata = dev_get_platdata(pdev->dev.parent);
	if (!pdata || !pdata->charger_pdata) {
		dev_err(&pdev->dev, "No platform data\n");
		return -ENODEV;
	}

	chg_pdata = pdata->charger_pdata;

	chg_extcon = devm_kzalloc(&pdev->dev, sizeof(*chg_extcon), GFP_KERNEL);
	if (!chg_extcon) {
		dev_err(&pdev->dev, "Memory allocation failed for chg_extcon\n");
		return -ENOMEM;
	}

	edev = devm_kzalloc(&pdev->dev, sizeof(*edev), GFP_KERNEL);
	if (!edev) {
		dev_err(&pdev->dev, "Memory allocation failed for edev\n");
		return -ENOMEM;
	}

	chg_extcon->charger = devm_kzalloc(&pdev->dev,
				sizeof(*(chg_extcon->charger)), GFP_KERNEL);
	if (!chg_extcon->charger) {
		dev_err(&pdev->dev, "Memory allocation failed for charger\n");
		return -ENOMEM;
	}

	charger = chg_extcon->charger;
	charger->ac_online = 0;
	charger->usb_online = 0;
	charger->status = 0;
	charger->is_100ma = 0;

	chg_extcon->edev = edev;
	chg_extcon->edev->name = (chg_pdata->ext_conn_name) ?
					chg_pdata->ext_conn_name :
					dev_name(&pdev->dev);
	chg_extcon->edev->supported_cable = max77660_excon_cable;

	charger->update_status =  chg_pdata->update_status;
	charger->wdt_timeout = chg_pdata->wdt_timeout;

	chg_extcon->dev = &pdev->dev;
	chg_extcon->parent = pdev->dev.parent;
	dev_set_drvdata(&pdev->dev, chg_extcon);

	chg_extcon->chg_irq = platform_get_irq(pdev, 0);

	chg_extcon->chg_wdt_irq = platform_get_irq(pdev, 1);
	max77660_ext = chg_extcon;

	ret = extcon_dev_register(chg_extcon->edev, NULL);
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to register extcon device\n");
		return ret;
	}

	/* Set initial state */
	ret = max77660_chg_extcon_cable_update(chg_extcon);
	if (ret < 0) {
		dev_err(&pdev->dev, "Cable init failed: %d\n", ret);
		goto out;
	}

	ret = request_threaded_irq(chg_extcon->chg_irq, NULL,
		max77660_chg_extcon_irq,
		IRQF_ONESHOT | IRQF_EARLY_RESUME, "max77660-charger",
		chg_extcon);
	if (ret < 0) {
		dev_err(chg_extcon->dev,
			"request irq %d failed: %dn", chg_extcon->chg_irq, ret);
		goto out;
	}

	ret = request_threaded_irq(chg_extcon->chg_wdt_irq, NULL,
		max77660_chg_wdt_irq,
		IRQF_ONESHOT | IRQF_EARLY_RESUME, "max77660-charger-wdt",
		chg_extcon);
	if (ret < 0) {
		dev_err(chg_extcon->dev, "request irq %d failed: %d\n",
			chg_extcon->chg_wdt_irq, ret);
		goto chg_irq_free;
	}

	ret = max77660_reg_clr_bits(chg_extcon->parent, MAX77660_CHG_SLAVE,
			MAX77660_CHARGER_CHGINTM, MAX77660_CHG_CHGINT_DC_UVP);
	if (ret < 0) {
		dev_err(chg_extcon->dev, "CHGINTM update failed: %d\n", ret);
		goto wdt_irq_free;
	}

	config.dev = &pdev->dev;
	config.init_data = chg_pdata->vbus_reg_init_data;
	config.driver_data = chg_extcon;

	chg_extcon->rdev = regulator_register(&max77660_vbus_desc, &config);
	if (IS_ERR(chg_extcon->rdev)) {
		ret = PTR_ERR(chg_extcon->rdev);
		dev_err(&pdev->dev, "Failed to register VBUS regulator: %d\n",
					ret);
		goto wdt_irq_free;
	}

	ret = max77660_init_charger_regulator(chg_extcon, chg_pdata);
	if (ret < 0) {
		dev_err(&pdev->dev, "Failed to register charger regulator: %d\n",
					ret);
		goto vbus_reg_err;
	}

	ret = max77660_psy_init(chg_extcon);
	if (ret < 0) {
		dev_err(&pdev->dev,
			"Charger power supply init failed %d\n", ret);
		goto psy_reg_err;
	}

	ret = max77660_charger_wdt(chg_extcon);
	if (ret < 0) {
		dev_err(&pdev->dev,
			"Charger watchdog timer init failed %d\n", ret);
		goto chg_wdt_err;
	}

	device_set_wakeup_capable(&pdev->dev, 1);
	return 0;

chg_wdt_err:
	power_supply_unregister(&charger->dc_psy);
	power_supply_unregister(&charger->usb_psy);
psy_reg_err:
	regulator_unregister(chg_extcon->chg_rdev);
vbus_reg_err:
	regulator_unregister(chg_extcon->rdev);
wdt_irq_free:
	free_irq(chg_extcon->chg_wdt_irq, chg_extcon);
chg_irq_free:
	free_irq(chg_extcon->chg_irq, chg_extcon);
out:
	extcon_dev_unregister(chg_extcon->edev);
	return ret;
}

static int max77660_chg_extcon_remove(struct platform_device *pdev)
{
	struct max77660_chg_extcon *chg_extcon = dev_get_drvdata(&pdev->dev);

	extcon_dev_unregister(chg_extcon->edev);
	power_supply_unregister(&chg_extcon->charger->dc_psy);
	power_supply_unregister(&chg_extcon->charger->usb_psy);
	free_irq(chg_extcon->chg_irq, chg_extcon);
	free_irq(chg_extcon->chg_wdt_irq, chg_extcon);
	regulator_unregister(chg_extcon->rdev);
	return 0;
}

#ifdef CONFIG_PM_SLEEP
static int max77660_chg_extcon_suspend(struct device *dev)
{
	struct max77660_chg_extcon *chg_extcon = dev_get_drvdata(dev);
	int ret;

	if (device_may_wakeup(dev)) {
		enable_irq_wake(chg_extcon->chg_irq);
		if (chg_extcon->charger->wdt_timeout)
			enable_irq_wake(chg_extcon->chg_wdt_irq);
	} else {
		if (chg_extcon->charger->wdt_timeout) {
			ret = max77660_reg_clr_bits(chg_extcon->parent,
				MAX77660_PWR_SLAVE,
				MAX77660_REG_GLOBAL_CFG1,
				MAX77660_GLBLCNFG1_ENCHGTL);
			if (ret < 0)
				dev_err(chg_extcon->dev,
					"GLBLCNFG1_ENCHGTL update failed: %d\n",
					 ret);
		}
	}
	return 0;
}

static int max77660_chg_extcon_resume(struct device *dev)
{
	struct max77660_chg_extcon *chg_extcon = dev_get_drvdata(dev);
	int ret;

	if (device_may_wakeup(dev)) {
		disable_irq_wake(chg_extcon->chg_irq);
		if (chg_extcon->charger->wdt_timeout)
			disable_irq_wake(chg_extcon->chg_wdt_irq);
	} else {
		if (chg_extcon->charger->wdt_timeout) {
			ret = max77660_reg_set_bits(chg_extcon->parent,
				MAX77660_PWR_SLAVE,
				MAX77660_REG_GLOBAL_CFG1,
				MAX77660_GLBLCNFG1_ENCHGTL);
			if (ret < 0)
				dev_err(chg_extcon->dev,
					"GLBLCNFG1_ENCHGTL update failed: %d\n",
					 ret);
		}
	}
	return 0;
};
#endif

static const struct dev_pm_ops max77660_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(max77660_chg_extcon_suspend,
				max77660_chg_extcon_resume)
};

static struct platform_driver max77660_chg_extcon_driver = {
	.probe = max77660_chg_extcon_probe,
	.remove = max77660_chg_extcon_remove,
	.driver = {
		.name = "max77660-charger-extcon",
		.owner = THIS_MODULE,
		.pm = &max77660_pm_ops,
	},
};

static int __init max77660_chg_extcon_driver_init(void)
{
	return platform_driver_register(&max77660_chg_extcon_driver);
}
subsys_initcall_sync(max77660_chg_extcon_driver_init);

static void __exit max77660_chg_extcon_driver_exit(void)
{
	platform_driver_unregister(&max77660_chg_extcon_driver);
}
module_exit(max77660_chg_extcon_driver_exit);

MODULE_DESCRIPTION("max77660 charger-extcon driver");
MODULE_AUTHOR("Syed Rafiuddin<srafiuddin@nvidia.com>");
MODULE_AUTHOR("Laxman Dewangan<ldewangan@nvidia.com>");
MODULE_ALIAS("platform:max77660-charger-extcon");
MODULE_LICENSE("GPL v2");