summaryrefslogtreecommitdiffstats
path: root/drivers/power/supply/cpcap-charger.c
blob: 891e1eb8e39d51029b0c89c060913739245f360c (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Motorola CPCAP PMIC battery charger driver
 *
 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
 *
 * Rewritten for Linux power framework with some parts based on
 * on earlier driver found in the Motorola Linux kernel:
 *
 * Copyright (C) 2009-2010 Motorola, Inc.
 */

#include <linux/atomic.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/interrupt.h>
#include <linux/notifier.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/power_supply.h>
#include <linux/regmap.h>

#include <linux/gpio/consumer.h>
#include <linux/usb/phy_companion.h>
#include <linux/phy/omap_usb.h>
#include <linux/usb/otg.h>
#include <linux/iio/consumer.h>
#include <linux/mfd/motorola-cpcap.h>

/*
 * CPCAP_REG_CRM register bits. For documentation of somewhat similar hardware,
 * see NXP "MC13783 Power Management and Audio Circuit Users's Guide"
 * MC13783UG.pdf chapter "8.5 Battery Interface Register Summary". The registers
 * and values for CPCAP are different, but some of the internal components seem
 * similar. Also see the Motorola Linux kernel cpcap-regbits.h. CPCAP_REG_CHRGR_1
 * bits that seem to describe the CRM register.
 */
#define CPCAP_REG_CRM_UNUSED_641_15	BIT(15)	/* 641 = register number */
#define CPCAP_REG_CRM_UNUSED_641_14	BIT(14)	/* 641 = register number */
#define CPCAP_REG_CRM_CHRG_LED_EN	BIT(13)	/* Charger LED */
#define CPCAP_REG_CRM_RVRSMODE		BIT(12)	/* USB VBUS output enable */
#define CPCAP_REG_CRM_ICHRG_TR1		BIT(11)	/* Trickle charge current */
#define CPCAP_REG_CRM_ICHRG_TR0		BIT(10)
#define CPCAP_REG_CRM_FET_OVRD		BIT(9)	/* 0 = hardware, 1 = FET_CTRL */
#define CPCAP_REG_CRM_FET_CTRL		BIT(8)	/* BPFET 1 if FET_OVRD set */
#define CPCAP_REG_CRM_VCHRG3		BIT(7)	/* Charge voltage bits */
#define CPCAP_REG_CRM_VCHRG2		BIT(6)
#define CPCAP_REG_CRM_VCHRG1		BIT(5)
#define CPCAP_REG_CRM_VCHRG0		BIT(4)
#define CPCAP_REG_CRM_ICHRG3		BIT(3)	/* Charge current bits */
#define CPCAP_REG_CRM_ICHRG2		BIT(2)
#define CPCAP_REG_CRM_ICHRG1		BIT(1)
#define CPCAP_REG_CRM_ICHRG0		BIT(0)

/* CPCAP_REG_CRM trickle charge voltages */
#define CPCAP_REG_CRM_TR(val)		(((val) & 0x3) << 10)
#define CPCAP_REG_CRM_TR_0A00		CPCAP_REG_CRM_TR(0x0)
#define CPCAP_REG_CRM_TR_0A24		CPCAP_REG_CRM_TR(0x1)
#define CPCAP_REG_CRM_TR_0A48		CPCAP_REG_CRM_TR(0x2)
#define CPCAP_REG_CRM_TR_0A72		CPCAP_REG_CRM_TR(0x4)

/*
 * CPCAP_REG_CRM charge voltages based on the ADC channel 1 values.
 * Note that these register bits don't match MC13783UG.pdf VCHRG
 * register bits.
 */
#define CPCAP_REG_CRM_VCHRG(val)	(((val) & 0xf) << 4)
#define CPCAP_REG_CRM_VCHRG_3V80	CPCAP_REG_CRM_VCHRG(0x0)
#define CPCAP_REG_CRM_VCHRG_4V10	CPCAP_REG_CRM_VCHRG(0x1)
#define CPCAP_REG_CRM_VCHRG_4V12	CPCAP_REG_CRM_VCHRG(0x2)
#define CPCAP_REG_CRM_VCHRG_4V15	CPCAP_REG_CRM_VCHRG(0x3)
#define CPCAP_REG_CRM_VCHRG_4V17	CPCAP_REG_CRM_VCHRG(0x4)
#define CPCAP_REG_CRM_VCHRG_4V20	CPCAP_REG_CRM_VCHRG(0x5)
#define CPCAP_REG_CRM_VCHRG_4V23	CPCAP_REG_CRM_VCHRG(0x6)
#define CPCAP_REG_CRM_VCHRG_4V25	CPCAP_REG_CRM_VCHRG(0x7)
#define CPCAP_REG_CRM_VCHRG_4V27	CPCAP_REG_CRM_VCHRG(0x8)
#define CPCAP_REG_CRM_VCHRG_4V30	CPCAP_REG_CRM_VCHRG(0x9)
#define CPCAP_REG_CRM_VCHRG_4V33	CPCAP_REG_CRM_VCHRG(0xa)
#define CPCAP_REG_CRM_VCHRG_4V35	CPCAP_REG_CRM_VCHRG(0xb)
#define CPCAP_REG_CRM_VCHRG_4V38	CPCAP_REG_CRM_VCHRG(0xc)
#define CPCAP_REG_CRM_VCHRG_4V40	CPCAP_REG_CRM_VCHRG(0xd)
#define CPCAP_REG_CRM_VCHRG_4V42	CPCAP_REG_CRM_VCHRG(0xe)
#define CPCAP_REG_CRM_VCHRG_4V44	CPCAP_REG_CRM_VCHRG(0xf)

/*
 * CPCAP_REG_CRM charge currents. These seem to match MC13783UG.pdf
 * values in "Table 8-3. Charge Path Regulator Current Limit
 * Characteristics" for the nominal values.
 */
#define CPCAP_REG_CRM_ICHRG(val)	(((val) & 0xf) << 0)
#define CPCAP_REG_CRM_ICHRG_0A000	CPCAP_REG_CRM_ICHRG(0x0)
#define CPCAP_REG_CRM_ICHRG_0A070	CPCAP_REG_CRM_ICHRG(0x1)
#define CPCAP_REG_CRM_ICHRG_0A177	CPCAP_REG_CRM_ICHRG(0x2)
#define CPCAP_REG_CRM_ICHRG_0A266	CPCAP_REG_CRM_ICHRG(0x3)
#define CPCAP_REG_CRM_ICHRG_0A355	CPCAP_REG_CRM_ICHRG(0x4)
#define CPCAP_REG_CRM_ICHRG_0A443	CPCAP_REG_CRM_ICHRG(0x5)
#define CPCAP_REG_CRM_ICHRG_0A532	CPCAP_REG_CRM_ICHRG(0x6)
#define CPCAP_REG_CRM_ICHRG_0A621	CPCAP_REG_CRM_ICHRG(0x7)
#define CPCAP_REG_CRM_ICHRG_0A709	CPCAP_REG_CRM_ICHRG(0x8)
#define CPCAP_REG_CRM_ICHRG_0A798	CPCAP_REG_CRM_ICHRG(0x9)
#define CPCAP_REG_CRM_ICHRG_0A886	CPCAP_REG_CRM_ICHRG(0xa)
#define CPCAP_REG_CRM_ICHRG_0A975	CPCAP_REG_CRM_ICHRG(0xb)
#define CPCAP_REG_CRM_ICHRG_1A064	CPCAP_REG_CRM_ICHRG(0xc)
#define CPCAP_REG_CRM_ICHRG_1A152	CPCAP_REG_CRM_ICHRG(0xd)
#define CPCAP_REG_CRM_ICHRG_1A596	CPCAP_REG_CRM_ICHRG(0xe)
#define CPCAP_REG_CRM_ICHRG_NO_LIMIT	CPCAP_REG_CRM_ICHRG(0xf)

/* CPCAP_REG_VUSBC register bits needed for VBUS */
#define CPCAP_BIT_VBUS_SWITCH		BIT(0)	/* VBUS boost to 5V */

enum {
	CPCAP_CHARGER_IIO_BATTDET,
	CPCAP_CHARGER_IIO_VOLTAGE,
	CPCAP_CHARGER_IIO_VBUS,
	CPCAP_CHARGER_IIO_CHRG_CURRENT,
	CPCAP_CHARGER_IIO_BATT_CURRENT,
	CPCAP_CHARGER_IIO_NR,
};

enum {
	CPCAP_CHARGER_DISCONNECTED,
	CPCAP_CHARGER_DETECTING,
	CPCAP_CHARGER_CHARGING,
	CPCAP_CHARGER_DONE,
};

struct cpcap_charger_ddata {
	struct device *dev;
	struct regmap *reg;
	struct list_head irq_list;
	struct delayed_work detect_work;
	struct delayed_work vbus_work;
	struct gpio_desc *gpio[2];		/* gpio_reven0 & 1 */

	struct iio_channel *channels[CPCAP_CHARGER_IIO_NR];

	struct power_supply *usb;

	struct phy_companion comparator;	/* For USB VBUS */
	unsigned int vbus_enabled:1;
	unsigned int feeding_vbus:1;
	atomic_t active;

	int status;
	int state;
	int voltage;
};

struct cpcap_interrupt_desc {
	int irq;
	struct list_head node;
	const char *name;
};

struct cpcap_charger_ints_state {
	bool chrg_det;
	bool rvrs_chrg;
	bool vbusov;

	bool chrg_se1b;
	bool rvrs_mode;
	bool chrgcurr2;
	bool chrgcurr1;
	bool vbusvld;

	bool battdetb;
};

static enum power_supply_property cpcap_charger_props[] = {
	POWER_SUPPLY_PROP_STATUS,
	POWER_SUPPLY_PROP_ONLINE,
	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
	POWER_SUPPLY_PROP_VOLTAGE_NOW,
	POWER_SUPPLY_PROP_CURRENT_NOW,
};

/* No battery always shows temperature of -40000 */
static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata)
{
	struct iio_channel *channel;
	int error, temperature;

	channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET];
	error = iio_read_channel_processed(channel, &temperature);
	if (error < 0) {
		dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);

		return false;
	}

	return temperature > -20000 && temperature < 60000;
}

static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata)
{
	struct iio_channel *channel;
	int error, value = 0;

	channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE];
	error = iio_read_channel_processed(channel, &value);
	if (error < 0) {
		dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);

		return 0;
	}

	return value;
}

static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata)
{
	struct iio_channel *channel;
	int error, value = 0;

	channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT];
	error = iio_read_channel_processed(channel, &value);
	if (error < 0) {
		dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);

		return 0;
	}

	return value;
}

static int cpcap_charger_get_property(struct power_supply *psy,
				      enum power_supply_property psp,
				      union power_supply_propval *val)
{
	struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);

	switch (psp) {
	case POWER_SUPPLY_PROP_STATUS:
		val->intval = ddata->status;
		break;
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
		val->intval = ddata->voltage;
		break;
	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
		if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
			val->intval = cpcap_charger_get_charge_voltage(ddata) *
				1000;
		else
			val->intval = 0;
		break;
	case POWER_SUPPLY_PROP_CURRENT_NOW:
		if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
			val->intval = cpcap_charger_get_charge_current(ddata) *
				1000;
		else
			val->intval = 0;
		break;
	case POWER_SUPPLY_PROP_ONLINE:
		val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int cpcap_charger_match_voltage(int voltage)
{
	switch (voltage) {
	case 0 ... 4100000 - 1: return 3800000;
	case 4100000 ... 4120000 - 1: return 4100000;
	case 4120000 ... 4150000 - 1: return 4120000;
	case 4150000 ... 4170000 - 1: return 4150000;
	case 4170000 ... 4200000 - 1: return 4170000;
	case 4200000 ... 4230000 - 1: return 4200000;
	case 4230000 ... 4250000 - 1: return 4230000;
	case 4250000 ... 4270000 - 1: return 4250000;
	case 4270000 ... 4300000 - 1: return 4270000;
	case 4300000 ... 4330000 - 1: return 4300000;
	case 4330000 ... 4350000 - 1: return 4330000;
	case 4350000 ... 4380000 - 1: return 4350000;
	case 4380000 ... 4400000 - 1: return 4380000;
	case 4400000 ... 4420000 - 1: return 4400000;
	case 4420000 ... 4440000 - 1: return 4420000;
	case 4440000: return 4440000;
	default: return 0;
	}
}

static int
cpcap_charger_get_bat_const_charge_voltage(struct cpcap_charger_ddata *ddata)
{
	union power_supply_propval prop;
	struct power_supply *battery;
	int voltage = ddata->voltage;
	int error;

	battery = power_supply_get_by_name("battery");
	if (battery) {
		error = power_supply_get_property(battery,
				POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
				&prop);
		if (!error)
			voltage = prop.intval;

		power_supply_put(battery);
	}

	return voltage;
}

static int cpcap_charger_set_property(struct power_supply *psy,
				      enum power_supply_property psp,
				      const union power_supply_propval *val)
{
	struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
	int voltage, batvolt;

	switch (psp) {
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
		voltage = cpcap_charger_match_voltage(val->intval);
		batvolt = cpcap_charger_get_bat_const_charge_voltage(ddata);
		if (voltage > batvolt)
			voltage = batvolt;
		ddata->voltage = voltage;
		schedule_delayed_work(&ddata->detect_work, 0);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int cpcap_charger_property_is_writeable(struct power_supply *psy,
					       enum power_supply_property psp)
{
	switch (psp) {
	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
		return 1;
	default:
		return 0;
	}
}

static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
					 bool enabled)
{
	if (!ddata->gpio[0])
		return;

	gpiod_set_value(ddata->gpio[0], enabled);
}

static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata,
					     bool enabled)
{
	if (!ddata->gpio[1])
		return;

	gpiod_set_value(ddata->gpio[1], enabled);
}

static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata,
				   int max_voltage, int charge_current,
				   int trickle_current)
{
	bool enable;
	int error;

	enable = (charge_current || trickle_current);
	dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable);

	if (!enable) {
		error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
					   0x3fff,
					   CPCAP_REG_CRM_FET_OVRD |
					   CPCAP_REG_CRM_FET_CTRL);
		if (error) {
			ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
			goto out_err;
		}

		ddata->status = POWER_SUPPLY_STATUS_DISCHARGING;

		return 0;
	}

	error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff,
				   CPCAP_REG_CRM_CHRG_LED_EN |
				   trickle_current |
				   CPCAP_REG_CRM_FET_OVRD |
				   CPCAP_REG_CRM_FET_CTRL |
				   max_voltage |
				   charge_current);
	if (error) {
		ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
		goto out_err;
	}

	ddata->status = POWER_SUPPLY_STATUS_CHARGING;

	return 0;

out_err:
	dev_err(ddata->dev, "%s failed with %i\n", __func__, error);

	return error;
}

static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata)
{
	int error, value = 0;
	struct iio_channel *channel =
		ddata->channels[CPCAP_CHARGER_IIO_VBUS];

	error = iio_read_channel_processed(channel, &value);
	if (error >= 0)
		return value > 3900 ? true : false;

	dev_err(ddata->dev, "error reading VBUS: %i\n", error);

	return false;
}

/* VBUS control functions for the USB PHY companion */
static void cpcap_charger_vbus_work(struct work_struct *work)
{
	struct cpcap_charger_ddata *ddata;
	bool vbus = false;
	int error;

	ddata = container_of(work, struct cpcap_charger_ddata,
			     vbus_work.work);

	if (ddata->vbus_enabled) {
		vbus = cpcap_charger_vbus_valid(ddata);
		if (vbus) {
			dev_info(ddata->dev, "VBUS already provided\n");

			return;
		}

		ddata->feeding_vbus = true;
		cpcap_charger_set_cable_path(ddata, false);
		cpcap_charger_set_inductive_path(ddata, false);

		error = cpcap_charger_set_state(ddata, 0, 0, 0);
		if (error)
			goto out_err;

		error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC,
					   CPCAP_BIT_VBUS_SWITCH,
					   CPCAP_BIT_VBUS_SWITCH);
		if (error)
			goto out_err;

		error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
					   CPCAP_REG_CRM_RVRSMODE,
					   CPCAP_REG_CRM_RVRSMODE);
		if (error)
			goto out_err;
	} else {
		error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC,
					   CPCAP_BIT_VBUS_SWITCH, 0);
		if (error)
			goto out_err;

		error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
					   CPCAP_REG_CRM_RVRSMODE, 0);
		if (error)
			goto out_err;

		cpcap_charger_set_cable_path(ddata, true);
		cpcap_charger_set_inductive_path(ddata, true);
		ddata->feeding_vbus = false;
	}

	return;

out_err:
	dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__,
		ddata->vbus_enabled ? "enable" : "disable", error);
}

static int cpcap_charger_set_vbus(struct phy_companion *comparator,
				  bool enabled)
{
	struct cpcap_charger_ddata *ddata =
		container_of(comparator, struct cpcap_charger_ddata,
			     comparator);

	ddata->vbus_enabled = enabled;
	schedule_delayed_work(&ddata->vbus_work, 0);

	return 0;
}

/* Charger interrupt handling functions */

static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata,
					struct cpcap_charger_ints_state *s)
{
	int val, error;

	error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
	if (error)
		return error;

	s->chrg_det = val & BIT(13);
	s->rvrs_chrg = val & BIT(12);
	s->vbusov = val & BIT(11);

	error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
	if (error)
		return error;

	s->chrg_se1b = val & BIT(13);
	s->rvrs_mode = val & BIT(6);
	s->chrgcurr2 = val & BIT(5);
	s->chrgcurr1 = val & BIT(4);
	s->vbusvld = val & BIT(3);

	error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
	if (error)
		return error;

	s->battdetb = val & BIT(6);

	return 0;
}

static void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata,
				       int state)
{
	const char *status;

	if (state > CPCAP_CHARGER_DONE) {
		dev_warn(ddata->dev, "unknown state: %i\n", state);

		return;
	}

	ddata->state = state;

	switch (state) {
	case CPCAP_CHARGER_DISCONNECTED:
		status = "DISCONNECTED";
		break;
	case CPCAP_CHARGER_DETECTING:
		status = "DETECTING";
		break;
	case CPCAP_CHARGER_CHARGING:
		status = "CHARGING";
		break;
	case CPCAP_CHARGER_DONE:
		status = "DONE";
		break;
	default:
		return;
	}

	dev_dbg(ddata->dev, "state: %s\n", status);
}

static int cpcap_charger_voltage_to_regval(int voltage)
{
	int offset;

	switch (voltage) {
	case 0 ... 4100000 - 1:
		return 0;
	case 4100000 ... 4200000 - 1:
		offset = 1;
		break;
	case 4200000 ... 4300000 - 1:
		offset = 0;
		break;
	case 4300000 ... 4380000 - 1:
		offset = -1;
		break;
	case 4380000 ... 4440000:
		offset = -2;
		break;
	default:
		return 0;
	}

	return ((voltage - 4100000) / 20000) + offset;
}

static void cpcap_charger_disconnect(struct cpcap_charger_ddata *ddata,
				     int state, unsigned long delay)
{
	int error;

	error = cpcap_charger_set_state(ddata, 0, 0, 0);
	if (error)
		return;

	cpcap_charger_update_state(ddata, state);
	power_supply_changed(ddata->usb);
	schedule_delayed_work(&ddata->detect_work, delay);
}

static void cpcap_usb_detect(struct work_struct *work)
{
	struct cpcap_charger_ddata *ddata;
	struct cpcap_charger_ints_state s;
	int error;

	ddata = container_of(work, struct cpcap_charger_ddata,
			     detect_work.work);

	error = cpcap_charger_get_ints_state(ddata, &s);
	if (error)
		return;

	/* Just init the state if a charger is connected with no chrg_det set */
	if (!s.chrg_det && s.chrgcurr1 && s.vbusvld) {
		cpcap_charger_update_state(ddata, CPCAP_CHARGER_DETECTING);

		return;
	}

	/*
	 * If battery voltage is higher than charge voltage, it may have been
	 * charged to 4.35V by Android. Try again in 10 minutes.
	 */
	if (cpcap_charger_get_charge_voltage(ddata) > ddata->voltage) {
		cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING,
					 HZ * 60 * 10);

		return;
	}

	/* Delay for 80ms to avoid vbus bouncing when usb cable is plugged in */
	usleep_range(80000, 120000);

	/* Throttle chrgcurr2 interrupt for charger done and retry */
	switch (ddata->state) {
	case CPCAP_CHARGER_CHARGING:
		if (s.chrgcurr2)
			break;
		if (s.chrgcurr1 && s.vbusvld) {
			cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DONE,
						 HZ * 5);
			return;
		}
		break;
	case CPCAP_CHARGER_DONE:
		if (!s.chrgcurr2)
			break;
		cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING,
					 HZ * 5);
		return;
	default:
		break;
	}

	if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) &&
	    s.chrgcurr1) {
		int max_current;
		int vchrg;

		if (cpcap_charger_battery_found(ddata))
			max_current = CPCAP_REG_CRM_ICHRG_1A596;
		else
			max_current = CPCAP_REG_CRM_ICHRG_0A532;

		vchrg = cpcap_charger_voltage_to_regval(ddata->voltage);
		error = cpcap_charger_set_state(ddata,
						CPCAP_REG_CRM_VCHRG(vchrg),
						max_current, 0);
		if (error)
			goto out_err;
		cpcap_charger_update_state(ddata, CPCAP_CHARGER_CHARGING);
	} else {
		error = cpcap_charger_set_state(ddata, 0, 0, 0);
		if (error)
			goto out_err;
		cpcap_charger_update_state(ddata, CPCAP_CHARGER_DISCONNECTED);
	}

	power_supply_changed(ddata->usb);
	return;

out_err:
	dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
}

static irqreturn_t cpcap_charger_irq_thread(int irq, void *data)
{
	struct cpcap_charger_ddata *ddata = data;

	if (!atomic_read(&ddata->active))
		return IRQ_NONE;

	schedule_delayed_work(&ddata->detect_work, 0);

	return IRQ_HANDLED;
}

static int cpcap_usb_init_irq(struct platform_device *pdev,
			      struct cpcap_charger_ddata *ddata,
			      const char *name)
{
	struct cpcap_interrupt_desc *d;
	int irq, error;

	irq = platform_get_irq_byname(pdev, name);
	if (irq < 0)
		return -ENODEV;

	error = devm_request_threaded_irq(ddata->dev, irq, NULL,
					  cpcap_charger_irq_thread,
					  IRQF_SHARED | IRQF_ONESHOT,
					  name, ddata);
	if (error) {
		dev_err(ddata->dev, "could not get irq %s: %i\n",
			name, error);

		return error;
	}

	d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
	if (!d)
		return -ENOMEM;

	d->name = name;
	d->irq = irq;
	list_add(&d->node, &ddata->irq_list);

	return 0;
}

static const char * const cpcap_charger_irqs[] = {
	/* REG_INT_0 */
	"chrg_det", "rvrs_chrg",

	/* REG_INT1 */
	"chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr2", "chrgcurr1", "vbusvld",

	/* REG_INT_3 */
	"battdetb",
};

static int cpcap_usb_init_interrupts(struct platform_device *pdev,
				     struct cpcap_charger_ddata *ddata)
{
	int i, error;

	for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) {
		error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]);
		if (error)
			return error;
	}

	return 0;
}

static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata)
{
	int i;

	for (i = 0; i < 2; i++) {
		ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
						      i, GPIOD_OUT_HIGH);
		if (IS_ERR(ddata->gpio[i])) {
			dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
				 i, PTR_ERR(ddata->gpio[i]));
			ddata->gpio[i] = NULL;
		}
	}
}

static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
{
	const char * const names[CPCAP_CHARGER_IIO_NR] = {
		"battdetb", "battp", "vbus", "chg_isense", "batti",
	};
	int error, i;

	for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) {
		ddata->channels[i] = devm_iio_channel_get(ddata->dev,
							  names[i]);
		if (IS_ERR(ddata->channels[i])) {
			error = PTR_ERR(ddata->channels[i]);
			goto out_err;
		}

		if (!ddata->channels[i]->indio_dev) {
			error = -ENXIO;
			goto out_err;
		}
	}

	return 0;

out_err:
	if (error != -EPROBE_DEFER)
		dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
			error);

	return error;
}

static const struct power_supply_desc cpcap_charger_usb_desc = {
	.name		= "usb",
	.type		= POWER_SUPPLY_TYPE_USB,
	.properties	= cpcap_charger_props,
	.num_properties	= ARRAY_SIZE(cpcap_charger_props),
	.get_property	= cpcap_charger_get_property,
	.set_property	= cpcap_charger_set_property,
	.property_is_writeable = cpcap_charger_property_is_writeable,
};

#ifdef CONFIG_OF
static const struct of_device_id cpcap_charger_id_table[] = {
	{
		.compatible = "motorola,mapphone-cpcap-charger",
	},
	{},
};
MODULE_DEVICE_TABLE(of, cpcap_charger_id_table);
#endif

static int cpcap_charger_probe(struct platform_device *pdev)
{
	struct cpcap_charger_ddata *ddata;
	const struct of_device_id *of_id;
	struct power_supply_config psy_cfg = {};
	int error;

	of_id = of_match_device(of_match_ptr(cpcap_charger_id_table),
				&pdev->dev);
	if (!of_id)
		return -EINVAL;

	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
	if (!ddata)
		return -ENOMEM;

	ddata->dev = &pdev->dev;
	ddata->voltage = 4200000;

	ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
	if (!ddata->reg)
		return -ENODEV;

	INIT_LIST_HEAD(&ddata->irq_list);
	INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
	INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work);
	platform_set_drvdata(pdev, ddata);

	error = cpcap_charger_init_iio(ddata);
	if (error)
		return error;

	atomic_set(&ddata->active, 1);

	psy_cfg.of_node = pdev->dev.of_node;
	psy_cfg.drv_data = ddata;

	ddata->usb = devm_power_supply_register(ddata->dev,
						&cpcap_charger_usb_desc,
						&psy_cfg);
	if (IS_ERR(ddata->usb)) {
		error = PTR_ERR(ddata->usb);
		dev_err(ddata->dev, "failed to register USB charger: %i\n",
			error);

		return error;
	}

	error = cpcap_usb_init_interrupts(pdev, ddata);
	if (error)
		return error;

	ddata->comparator.set_vbus = cpcap_charger_set_vbus;
	error = omap_usb2_set_comparator(&ddata->comparator);
	if (error == -ENODEV) {
		dev_info(ddata->dev, "charger needs phy, deferring probe\n");
		return -EPROBE_DEFER;
	}

	cpcap_charger_init_optional_gpios(ddata);

	schedule_delayed_work(&ddata->detect_work, 0);

	return 0;
}

static int cpcap_charger_remove(struct platform_device *pdev)
{
	struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev);
	int error;

	atomic_set(&ddata->active, 0);
	error = omap_usb2_set_comparator(NULL);
	if (error)
		dev_warn(ddata->dev, "could not clear USB comparator: %i\n",
			 error);

	error = cpcap_charger_set_state(ddata, 0, 0, 0);
	if (error)
		dev_warn(ddata->dev, "could not clear charger: %i\n",
			 error);
	cancel_delayed_work_sync(&ddata->vbus_work);
	cancel_delayed_work_sync(&ddata->detect_work);

	return 0;
}

static struct platform_driver cpcap_charger_driver = {
	.probe = cpcap_charger_probe,
	.driver	= {
		.name	= "cpcap-charger",
		.of_match_table = of_match_ptr(cpcap_charger_id_table),
	},
	.remove	= cpcap_charger_remove,
};
module_platform_driver(cpcap_charger_driver);

MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:cpcap-charger");