summaryrefstree
path: root/kernel/drivers/hwmon/qti_amoled_ecm.c
blob: a7fea80975002c0ecc6c4cb5942ac1dc01074157 (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
 */

#define pr_fmt(fmt)	"AMOLED_ECM: %s: " fmt, __func__

#include <linux/device.h>
#include <linux/errno.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/nvmem-consumer.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/workqueue.h>

#include <drm/drm_panel.h>

/* AMOLED AB register definitions */
#define AB_REVISION2				0x01

/* AMOLED ECM register definitions */
#define AB_ECM_EN_CTL				0xA0
 #define ECM_EN					BIT(7)

#define AB_ECM_COUNTER_CTL			0xA1
 #define ECM_COUNTER_START			BIT(7)

/* AMOLED ECM SDAM Offsets */
#define ECM_SDAM_START_BASE			0x40
#define ECM_SDAMX_SAMPLE_START_ADDR		0x46

#define ECM_FAULT_LOG				0x48
#define ECM_ROUTINE_LOG				0x49

#define ECM_ACTIVE_SDAM				0x4D
 #define ECM_SDAM0_ACTIVE			BIT(0)
 #define ECM_SDAM1_ACTIVE			BIT(1)

#define ECM_SAMPLE_CNT_LSB			0x4E
#define ECM_SAMPLE_CNT_MSB			0x4F

#define ECM_STATUS_SET				0x50
#define ECM_STATUS_CLR				0x51
 #define ECM_ONGOING				BIT(0)
 #define ECM_DONE				BIT(1)
 #define ECM_ABORT				BIT(2)
 #define ECM_SDAM0_FULL				BIT(3)
 #define ECM_SDAM1_FULL				BIT(4)

#define ECM_SDAM0_INDEX				0x52
#define ECM_SDAM1_INDEX				0x53

#define ECM_MODE				0x54
 #define ECM_CONTINUOUS				0
 #define ECM_N_ESWIRE				1
 #define ECM_M_ASWIRE				2
 #define ECM_ESWIRE_ASWIRE			3
 #define ECM_USE_TIMER				4

#define ECM_N_ESWIRE_COUNT_LSB			0x55
#define ECM_N_ESWIRE_COUNT_MSB			0x56
#define ECM_M_ASWIRE_COUNT_LSB			0x57
#define ECM_M_ASWIRE_COUNT_MSB			0x58
#define ECM_ESWIRE_ASWIRE_SKIP_COUNT_LSB	0x59
#define ECM_ESWIRE_ASWIRE_SKIP_COUNT_MSB	0x5A
#define ECM_TIMER_LSB				0x5B
#define ECM_TIMER_MSB				0x5C
#define ECM_TIMER_SKIP_LSB			0x5D
#define ECM_TIMER_SKIP_MSB			0x5E

#define ECM_SEND_IRQ				0x5F
 #define SEND_SDAM0_IRQ				BIT(0)
 #define SEND_SDAM1_IRQ				BIT(1)

#define ECM_WRITE_TO_SDAM			0x60
 #define WRITE_SDAM0_DATA			BIT(0)
 #define WRITE_SDAM1_DATA			BIT(1)
 #define OVERWRITE_SDAM0_DATA			BIT(4)
 #define OVERWRITE_SDAM1_DATA			BIT(5)

#define ECM_AVERAGE_LSB				0x61
#define ECM_AVERAGE_MSB				0x62
#define ECM_MIN_LSB				0x63
#define ECM_MIN_MSB				0x64
#define ECM_MAX_LSB				0x65
#define ECM_MAX_MSB				0x66

#define ECM_SDAM0_SAMPLE_START_ADDR		0x6C
#define ECM_SDAM_SAMPLE_END_ADDR		0xBF

/* ECM specific definitions */
#define ECM_SAMPLE_GAIN_V1			15
#define ECM_SAMPLE_GAIN_V2			16
#define ECM_MIN_M_SAMPLES			10
#define AMOLED_AB_REVISION_1P0			0
#define AMOLED_AB_REVISION_2P0			1

enum amoled_ecm_mode {
	ECM_MODE_CONTINUOUS = 0,
	ECM_MODE_MULTI_FRAMES,
	ECM_MODE_IDLE,
};

struct amoled_ecm_sdam_config {
	u8 reg;
	u8 reset_val;
};

/**
 * struct amoled_ecm_sdam - AMOLED ECM sdam data structure
 * @nvmem:		Pointer to nvmem device
 * @start_addr:		Start address of ECM samples in SDAM
 * @irq_name:		Interrupt name for SDAM
 * @irq:		Interrupt associated with the SDAM
 */
struct amoled_ecm_sdam {
	struct nvmem_device	*nvmem;
	u32			start_addr;
	const char		*irq_name;
	int			irq;
};

/**
 * struct amoled_ecm_data - Structure for AMOLED ECM data
 * @m_cumulative:	Cumulative of M sample values
 * @num_m_samples:	Number of M samples available
 * @time_period_ms:	Time period(in milli seconds) for ECM request
 * @frames:		Number of frames for ECM request
 * @avg_current:	AMOLED ECM average calculated
 * @mode:		AMOLED ECM mode of operation
 */
struct amoled_ecm_data {
	unsigned long long	m_cumulative;
	u32			num_m_samples;
	u32			time_period_ms;
	u16			frames;
	u16			avg_current;
	enum amoled_ecm_mode	mode;
};

/**
 * struct amoled ecm - Structure for AMOLED ECM device
 * @regmap:		Pointer for regmap structure
 * @dev:		Pointer for AMOLED ECM device
 * @data:		AMOLED ECM data structure
 * @sdam:		Pointer for array of ECM sdams
 * @sdam_lock:		Locking for mutual exclusion
 * @average_work:	Delayed work to calculate ECM average
 * @drm_notifier:	The notifier block for receiving DRM notifications
 * @active_panel:	Active DRM panel which sends DRM notifications
 * @num_sdams:		Number of SDAMs used for AMOLED ECM
 * @base:		Base address of the AMOLED ECM module
 * @ab_revision:	Revision of the AMOLED AB module
 * @enable:		Flag to enable/disable AMOLED ECM
 * @abort:		Flag to indicated AMOLED ECM has aborted
 * @reenable:		Flag to reenable ECM when display goes unblank
 */
struct amoled_ecm {
	struct regmap		*regmap;
	struct device		*dev;
	struct amoled_ecm_data	data;
	struct amoled_ecm_sdam	*sdam;
	struct mutex		sdam_lock;
	struct delayed_work	average_work;
	struct notifier_block	drm_notifier;
	struct drm_panel	*active_panel;
	u32			num_sdams;
	u32			base;
	u8			ab_revision;
	bool			enable;
	bool			abort;
	bool			reenable;
};

static struct amoled_ecm_sdam_config ecm_reset_config[] = {
	{ ECM_FAULT_LOG,	0x00 },
	{ ECM_ROUTINE_LOG,	0x00 },
	{ ECM_ACTIVE_SDAM,	0x01 },
	{ ECM_SAMPLE_CNT_LSB,	0x00 },
	{ ECM_SAMPLE_CNT_MSB,	0x00 },
	{ ECM_STATUS_SET,	0x00 },
	{ ECM_STATUS_CLR,	0xFF },
	{ ECM_SDAM0_INDEX,	0x6C },
	{ ECM_SDAM1_INDEX,	0x46 },
	{ ECM_MODE,		0x00 },
	/* Valid only when ECM uses 2 SDAMs */
	{ ECM_SEND_IRQ,		0x03 },
	{ ECM_WRITE_TO_SDAM,	0x03 }
};

static int ecm_nvmem_device_write(struct nvmem_device *nvmem,
		       unsigned int offset,
		       size_t bytes, void *buf)
{
	size_t i;
	u8 *ptr = buf;

	for (i = 0; i < bytes; i++)
		pr_debug("Wrote %#x to %#x\n", *ptr++, offset + i);

	return nvmem_device_write(nvmem, offset, bytes, buf);
}

static int ecm_reset_sdam_config(struct amoled_ecm *ecm)
{
	int rc, i;

	for (i = 0; i < ARRAY_SIZE(ecm_reset_config); i++) {
		rc = ecm_nvmem_device_write(ecm->sdam[0].nvmem,
				ecm_reset_config[i].reg,
				1, &ecm_reset_config[i].reset_val);
		if (rc < 0) {
			pr_err("Failed to write %u to SDAM, rc=%d\n",
				ecm_reset_config[i].reg, rc);
			return rc;
		}
	}

	usleep_range(10000, 12000);

	return rc;
}

static int amoled_ecm_enable(struct amoled_ecm *ecm)
{
	struct amoled_ecm_data *data = &ecm->data;
	int rc;

	if (data->frames) {
		rc = ecm_nvmem_device_write(ecm->sdam[0].nvmem,
				ECM_N_ESWIRE_COUNT_LSB, 2, &data->frames);
		if (rc < 0) {
			pr_err("Failed to write swire count to SDAM, rc=%d\n",
				rc);
			return rc;
		}

		data->mode = ECM_MODE_MULTI_FRAMES;
	} else {
		if (!data->time_period_ms)
			return -EINVAL;

		data->mode = ECM_MODE_CONTINUOUS;
	}

	if ((ecm->ab_revision != AMOLED_AB_REVISION_1P0) &&
			(ecm->ab_revision != AMOLED_AB_REVISION_2P0)) {
		pr_err("ECM is not supported for AB version %u\n",
			ecm->ab_revision);
		return -ENODEV;
	}

	rc = ecm_reset_sdam_config(ecm);
	if (rc < 0) {
		pr_err("Failed to reset ECM SDAM configuration, rc=%d\n", rc);
		return rc;
	}

	rc = ecm_nvmem_device_write(ecm->sdam[0].nvmem, ECM_MODE, 1,
				&data->mode);
	if (rc < 0) {
		pr_err("Failed to write ECM mode to SDAM, rc=%d\n", rc);
		return rc;
	}

	rc = regmap_write(ecm->regmap, ecm->base + AB_ECM_EN_CTL, ECM_EN);
	if (rc < 0) {
		pr_err("Failed to enable ECM, rc=%d\n", rc);
		return rc;
	}

	rc = regmap_write(ecm->regmap, ecm->base + AB_ECM_COUNTER_CTL,
		ECM_COUNTER_START);
	if (rc < 0) {
		pr_err("Failed to enable ECM counter, rc=%d\n", rc);
		return rc;
	}

	if (data->mode == ECM_MODE_CONTINUOUS)
		schedule_delayed_work(&ecm->average_work,
			msecs_to_jiffies(data->time_period_ms));

	ecm->enable = true;

	return rc;
}

static int amoled_ecm_disable(struct amoled_ecm *ecm)
{
	int rc;

	rc = regmap_write(ecm->regmap, ecm->base + AB_ECM_COUNTER_CTL, 0);
	if (rc < 0) {
		pr_err("Failed to disable ECM counter, rc=%d\n", rc);
		return rc;
	}

	rc = regmap_write(ecm->regmap, ecm->base + AB_ECM_EN_CTL, 0);
	if (rc < 0) {
		pr_err("Failed to disable ECM, rc=%d\n", rc);
		return rc;
	}

	rc = ecm_nvmem_device_write(ecm->sdam[0].nvmem, ECM_AVERAGE_LSB, 2,
				&ecm->data.avg_current);
	if (rc < 0) {
		pr_err("Failed to write ECM average to SDAM, rc=%d\n", rc);
		return rc;
	}
	pr_debug("ECM_AVERAGE:%u\n", ecm->data.avg_current);

	cancel_delayed_work(&ecm->average_work);

	ecm->data.avg_current = 0;
	ecm->data.m_cumulative = 0;
	ecm->data.num_m_samples = 0;
	ecm->data.mode = ECM_MODE_IDLE;

	ecm->abort = false;
	ecm->enable = false;

	return rc;
}

static void ecm_average_work(struct work_struct *work)
{
	struct amoled_ecm *ecm = container_of(work, struct amoled_ecm,
			average_work.work);
	struct amoled_ecm_data *data = &ecm->data;

	mutex_lock(&ecm->sdam_lock);

	if (!data->num_m_samples || !data->m_cumulative) {
		pr_warn("Invalid data, num_m_samples=%u m_cumulative:%u\n",
			data->num_m_samples, data->m_cumulative);
		data->avg_current = -EINVAL;
	} else {
		data->avg_current = data->m_cumulative / data->num_m_samples;
		pr_debug("avg_current=%u mA\n", data->avg_current);
	}

	data->m_cumulative = 0;
	data->num_m_samples = 0;

	mutex_unlock(&ecm->sdam_lock);

	/*
	 * If ECM is not aborted and still enabled, run it one more time
	 */

	if (!ecm->abort && ecm->enable)
		schedule_delayed_work(&ecm->average_work,
			msecs_to_jiffies(ecm->data.time_period_ms));
}

static ssize_t enable_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	struct amoled_ecm *ecm = dev_get_drvdata(dev);

	return scnprintf(buf, PAGE_SIZE, "%d\n", ecm->enable);
}

static ssize_t enable_store(struct device *dev,
			struct device_attribute *attr,
			const char *buf, size_t count)
{
	struct amoled_ecm *ecm = dev_get_drvdata(dev);
	bool val;
	int rc;

	rc = kstrtobool(buf, &val);
	if (rc < 0)
		return rc;

	if (ecm->enable == val) {
		pr_err("AMOLED ECM is already %s\n",
			val ? "enabled" : "disabled");
		return -EINVAL;
	}

	if (val) {
		rc = amoled_ecm_enable(ecm);
		if (rc < 0) {
			pr_err("Failed to enable AMOLED ECM, rc=%d\n", rc);
			return rc;
		}
	} else {
		rc = amoled_ecm_disable(ecm);
		if (rc < 0) {
			pr_err("Failed to disable AMOLED ECM, rc=%d\n", rc);
			return rc;
		}

		ecm->data.frames = 0;
		ecm->data.time_period_ms = 0;
	}

	return count;
}
static DEVICE_ATTR_RW(enable);

static ssize_t frames_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	struct amoled_ecm *ecm = dev_get_drvdata(dev);

	return scnprintf(buf, PAGE_SIZE, "%u\n", ecm->data.frames);
}

static ssize_t frames_store(struct device *dev,
			struct device_attribute *attr,
			const char *buf, size_t count)
{
	struct amoled_ecm *ecm = dev_get_drvdata(dev);
	u16 val;
	int rc;

	if (ecm->enable) {
		pr_err("Failed to configure frames, ECM is already running\n");
		return -EINVAL;
	}

	rc = kstrtou16(buf, 0, &val);
	if ((rc < 0) || !val)
		return -EINVAL;

	ecm->data.frames = val;

	return count;
}
static DEVICE_ATTR_RW(frames);

static ssize_t time_period_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct amoled_ecm *ecm = dev_get_drvdata(dev);

	return scnprintf(buf, PAGE_SIZE, "%u\n", ecm->data.time_period_ms);
}

static ssize_t time_period_store(struct device *dev,
			struct device_attribute *attr,
			const char *buf, size_t count)
{
	struct amoled_ecm *ecm = dev_get_drvdata(dev);
	u32 val;
	int rc;

	if (ecm->enable) {
		pr_err("Failed to configure time_period, ECM is already running\n");
		return -EINVAL;
	}

	rc = kstrtou32(buf, 0, &val);
	if ((rc < 0) || !val)
		return -EINVAL;

	ecm->data.time_period_ms = val;

	return count;
}
static DEVICE_ATTR_RW(time_period);

static ssize_t avg_current_show(struct device *dev,
			struct device_attribute *attr, char *buf)
{
	struct amoled_ecm *ecm = dev_get_drvdata(dev);

	return scnprintf(buf, PAGE_SIZE, "%u\n", ecm->data.avg_current);
}
static DEVICE_ATTR_RO(avg_current);

static struct attribute *amoled_ecm_attrs[] = {
	&dev_attr_enable.attr,
	&dev_attr_frames.attr,
	&dev_attr_time_period.attr,
	&dev_attr_avg_current.attr,
	NULL,
};

static const struct attribute_group amoled_ecm_group = {
	.name	= "amoled_ecm",
	.attrs	= amoled_ecm_attrs,
};
__ATTRIBUTE_GROUPS(amoled_ecm);

static int get_sdam_from_irq(struct amoled_ecm *ecm, int irq)
{
	int i;

	for (i = 0; i < ecm->num_sdams; i++)
		if (ecm->sdam[i].irq == irq)
			return i;

	return -ENOENT;
}

static int handle_ecm_abort(struct amoled_ecm *ecm)
{
	struct amoled_ecm_data *data = &ecm->data;
	int rc;
	u8 mode = data->mode;

	switch (mode) {
	case ECM_MODE_MULTI_FRAMES:
		data->avg_current = -EIO;
		break;
	case ECM_MODE_CONTINUOUS:
		if (data->num_m_samples < ECM_MIN_M_SAMPLES) {
			data->avg_current = -EIO;
			break;
		}

		ecm->abort = true;
		schedule_delayed_work(&ecm->average_work, 0);
		break;
	default:
		data->avg_current = -EINVAL;
		return -EINVAL;
	}

	rc = amoled_ecm_disable(ecm);
	if (rc < 0)
		pr_err("Failed to disable AMOLED ECM, rc=%d\n", rc);

	return rc;
}

static irqreturn_t sdam_full_irq_handler(int irq, void *_ecm)
{
	struct amoled_ecm *ecm = _ecm;
	struct amoled_ecm_data *data = &ecm->data;
	u64 cumulative = 0, m_sample;
	int rc, i, sdam_num, sdam_start, num_ecm_samples, max_samples;
	u16 ecm_sample, gain;
	u8 buf[2], int_status, sdam_index, overwrite;

	sdam_num = get_sdam_from_irq(ecm, irq);
	if (sdam_num < 0) {
		pr_err("Invalid SDAM interrupt, err=%d\n", sdam_num);
		return IRQ_HANDLED;
	}

	rc = nvmem_device_read(ecm->sdam[0].nvmem, ECM_STATUS_SET, 1,
			&int_status);
	if (rc < 0) {
		pr_err("Failed to read interrupt status from SDAM, rc=%d\n",
			rc);
		return IRQ_HANDLED;
	}

	pr_debug("ECM_STATUS_SET: %#x\n", int_status);

	if (data->mode != ECM_MODE_CONTINUOUS &&
		data->mode != ECM_MODE_MULTI_FRAMES)
		return IRQ_HANDLED;

	if (int_status & ECM_ABORT) {
		rc = handle_ecm_abort(ecm);
		if (rc < 0) {
			pr_err("Failed to handle ECM_ABORT interrupt, rc=%d\n",
				rc);
			return IRQ_HANDLED;
		}
	}

	rc = nvmem_device_read(ecm->sdam[0].nvmem,
			(ECM_SDAM0_INDEX + sdam_num), 1, &sdam_index);
	if (rc < 0) {
		pr_err("Failed to read SDAM index, rc=%d\n", rc);
		goto irq_exit;
	}

	pr_debug("sdam_num:%d sdam_index:%#x\n", sdam_num, sdam_index);

	sdam_start = ecm->sdam[sdam_num].start_addr;
	max_samples = (ECM_SDAM_SAMPLE_END_ADDR + 1 - sdam_start) / 2;
	num_ecm_samples = (sdam_index + 1 - sdam_start) / 2;

	if (!num_ecm_samples || (num_ecm_samples > max_samples)) {
		pr_err("Incorrect number of ECM samples, num_ecm_samples:%d max_samples:%d\n",
				num_ecm_samples, max_samples);
		return IRQ_HANDLED;
	}

	mutex_lock(&ecm->sdam_lock);

	rc = nvmem_device_read(ecm->sdam[0].nvmem, ECM_WRITE_TO_SDAM, 1,
		&overwrite);
	if (rc < 0) {
		pr_err("Failed to read ECM_WRITE_TO_SDAM from SDAM, rc=%d\n",
			rc);
		goto irq_exit;
	}

	overwrite &= ~(OVERWRITE_SDAM0_DATA << sdam_num);
	rc = ecm_nvmem_device_write(ecm->sdam[0].nvmem, ECM_WRITE_TO_SDAM,
		1, &overwrite);
	if (rc < 0) {
		pr_err("Failed to write ECM_WRITE_TO_SDAM to SDAM, rc=%d\n",
			rc);
		goto irq_exit;
	}

	/*
	 * For AMOLED AB peripheral,
	 * Revision 1.0:
	 * ECM measured current = 15 times of each LSB
	 *
	 * Revision 2.0:
	 * ECM measured current = 16 times of each LSB
	 */

	if (ecm->ab_revision == AMOLED_AB_REVISION_1P0)
		gain = ECM_SAMPLE_GAIN_V1;
	else
		gain = ECM_SAMPLE_GAIN_V2;

	for (i = sdam_start; i < sdam_index; i += 2) {
		rc = nvmem_device_read(ecm->sdam[sdam_num].nvmem, i, 2, buf);
		if (rc < 0) {
			pr_err("Failed to read SDAM sample, rc=%d\n", rc);
			goto irq_exit;
		}

		ecm_sample = (buf[1] << 8) | buf[0];

		cumulative += ((ecm_sample * 1000) / gain) / 1000;
	}

	overwrite |= (OVERWRITE_SDAM0_DATA << sdam_num);
	rc = ecm_nvmem_device_write(ecm->sdam[0].nvmem, ECM_WRITE_TO_SDAM,
		1, &overwrite);
	if (rc < 0) {
		pr_err("Failed to write ECM_WRITE_TO_SDAM to SDAM, rc=%d\n",
			rc);
		goto irq_exit;
	}

	if (!cumulative) {
		pr_err("Error, No ECM samples captured. Cumulative:%lu\n",
			cumulative);
		goto irq_exit;
	}

	m_sample = cumulative / num_ecm_samples;
	data->m_cumulative += m_sample;
	data->num_m_samples++;

	buf[0] = (ECM_SDAM0_FULL << sdam_num);
	rc = ecm_nvmem_device_write(ecm->sdam[0].nvmem, ECM_STATUS_CLR, 1,
			&buf[0]);
	if (rc < 0) {
		pr_err("Failed to clear interrupt status in SDAM, rc=%d\n",
			rc);
		goto irq_exit;
	}

	if ((data->mode == ECM_MODE_MULTI_FRAMES) &&
			(sdam_index < max_samples))
		schedule_delayed_work(&ecm->average_work, 0);

irq_exit:
	mutex_unlock(&ecm->sdam_lock);

	return IRQ_HANDLED;
}

#ifdef CONFIG_DRM
static int amoled_ecm_parse_panel_dt(struct amoled_ecm *ecm)
{
	struct device_node *np = ecm->dev->of_node;
	struct device_node *pnode;
	struct drm_panel *panel;
	int i, count;

	count = of_count_phandle_with_args(np, "display-panels", NULL);
	if (count <= 0)
		return 0;

	for (i = 0; i < count; i++) {
		pnode = of_parse_phandle(np, "display-panels", i);
		panel = of_drm_find_panel(pnode);
		of_node_put(pnode);
		if (!IS_ERR(panel)) {
			ecm->active_panel = panel;
			return 0;
		}
	}

	return PTR_ERR(panel);
}
#else
static inline int amoled_ecm_parse_panel_dt(struct amoled_ecm *ecm)
{
	return 0;
}
#endif

static int amoled_ecm_parse_dt(struct amoled_ecm *ecm)
{
	int rc = 0, i;
	u32 val;
	u8 buf[20];

	rc = of_property_read_u32(ecm->dev->of_node, "reg", &val);
	if (rc < 0) {
		pr_err("Failed to get reg, rc = %d\n", rc);
		return rc;
	}
	ecm->base = val;

	rc = of_property_count_strings(ecm->dev->of_node, "nvmem-names");
	if (rc < 0) {
		pr_err("Could not find nvmem device\n");
		return rc;
	}
	ecm->num_sdams = rc;

	ecm->sdam = devm_kcalloc(ecm->dev, ecm->num_sdams,
				sizeof(*ecm->sdam), GFP_KERNEL);
	if (!ecm->sdam)
		return -ENOMEM;

	for (i = 0; i < ecm->num_sdams; i++) {
		scnprintf(buf, sizeof(buf), "ecm-sdam%d", i);

		rc = of_irq_get_byname(ecm->dev->of_node, buf);
		if (rc < 0) {
			pr_err("Failed to get irq for ecm sdam, err=%d\n", rc);
			return -EINVAL;
		}

		ecm->sdam[i].irq_name = devm_kstrdup(ecm->dev, buf,
						GFP_KERNEL);
		if (!ecm->sdam[i].irq_name)
			return -ENOMEM;

		ecm->sdam[i].irq = rc;

		scnprintf(buf, sizeof(buf), "amoled-ecm-sdam%d", i);

		ecm->sdam[i].nvmem = devm_nvmem_device_get(ecm->dev, buf);
		if (IS_ERR(ecm->sdam[i].nvmem)) {
			rc = PTR_ERR(ecm->sdam[i].nvmem);
			if (rc != -EPROBE_DEFER)
				pr_err("Failed to get nvmem device, rc=%d\n",
					rc);
			ecm->sdam[i].nvmem = NULL;
			return rc;
		}
	}

	rc = amoled_ecm_parse_panel_dt(ecm);
	if (rc && rc != -EPROBE_DEFER)
		pr_err("failed to get active panel, rc=%d\n", rc);

	return rc;
}

#ifdef CONFIG_DRM
static int drm_notifier_callback(struct notifier_block *nb,
		unsigned long event, void *data)
{
	struct amoled_ecm *ecm = container_of(nb,
			struct amoled_ecm, drm_notifier);
	struct drm_panel_notifier *evdata = data;
	int blank, rc;

	pr_debug("DRM event received: %d\n", event);
	if (event != DRM_PANEL_EVENT_BLANK)
		return NOTIFY_DONE;

	blank = *(int *)evdata->data;
	if (blank == DRM_PANEL_BLANK_POWERDOWN && ecm->enable) {
		rc = amoled_ecm_disable(ecm);
		if (rc < 0) {
			pr_err("Failed to disable ECM for display BLANK, rc=%d\n",
					rc);
			return rc;
		}

		ecm->reenable = true;
		pr_debug("Disabled ECM for display BLANK\n");
	} else if (blank == DRM_PANEL_BLANK_UNBLANK && ecm->reenable) {
		rc = amoled_ecm_enable(ecm);
		if (rc < 0) {
			pr_err("Failed to reenable ECM for display UNBLANK, rc=%d\n",
					rc);
			return rc;
		}

		ecm->reenable = false;
		pr_debug("Enabled ECM for display UNBLANK\n");
	}

	return NOTIFY_DONE;
}

static int qti_amoled_register_drm_notifier(struct amoled_ecm *ecm)
{
	int rc = 0;

	if (ecm->active_panel) {
		ecm->drm_notifier.notifier_call = drm_notifier_callback;
		rc = drm_panel_notifier_register(ecm->active_panel,
				&ecm->drm_notifier);
		if (rc < 0)
			pr_err("failed to register DRM notifier, rc=%d\n", rc);
	}

	return rc;
}

static int qti_amoled_unregister_drm_notifier(struct amoled_ecm *ecm)
{
	if (ecm->active_panel)
		return drm_panel_notifier_unregister(ecm->active_panel,
				&ecm->drm_notifier);

	return 0;
}
#else
static inline int drm_notifier_callback(struct notifier_block *nb,
		unsigned long event, void *data)
{
	return NOTIFY_DONE;
}

static inline int qti_amoled_register_drm_notifier(struct amoled_ecm *ecm)
{
	return 0;
}

static inline int qti_amoled_unregister_drm_notifier(struct amoled_ecm *ecm)
{
	return 0;
}
#endif

static int qti_amoled_ecm_probe(struct platform_device *pdev)
{
	struct device *hwmon_dev;
	struct amoled_ecm *ecm;
	int rc, i;
	unsigned int temp;

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

	ecm->dev = &pdev->dev;

	ecm->regmap = dev_get_regmap(pdev->dev.parent, NULL);
	if (!ecm->regmap) {
		dev_err(&pdev->dev, "Failed to get regmap\n");
		return -EINVAL;
	}

	rc = amoled_ecm_parse_dt(ecm);
	if (rc < 0) {
		if (rc != -EPROBE_DEFER)
			dev_err(&pdev->dev, "Failed to parse AMOLED ECM rc=%d\n",
				rc);
		return rc;
	}

	rc = regmap_read(ecm->regmap, ecm->base + AB_REVISION2, &temp);
	if (rc < 0) {
		dev_err(&pdev->dev, "Failed to read AB revision, rc=%d\n", rc);
		return rc;
	}
	ecm->ab_revision = temp;

	ecm->enable = false;
	ecm->abort = false;

	ecm->data.m_cumulative = 0;
	ecm->data.num_m_samples = 0;
	ecm->data.time_period_ms = 0;
	ecm->data.frames = 0;
	ecm->data.avg_current = 0;
	ecm->data.mode = ECM_MODE_IDLE;

	INIT_DELAYED_WORK(&ecm->average_work, ecm_average_work);

	mutex_init(&ecm->sdam_lock);

	dev_set_drvdata(ecm->dev, ecm);

	for (i = 0; i < ecm->num_sdams; i++) {
		rc = devm_request_threaded_irq(ecm->dev, ecm->sdam[i].irq,
				NULL, sdam_full_irq_handler,
				IRQF_ONESHOT, ecm->sdam[i].irq_name, ecm);
		if (rc < 0) {
			dev_err(&pdev->dev, "Failed to request IRQ(%s), rc=%d\n",
				ecm->sdam[i].irq_name, rc);
			return rc;
		}

		ecm->sdam[i].start_addr = i ? ECM_SDAMX_SAMPLE_START_ADDR
				: ECM_SDAM0_SAMPLE_START_ADDR;
	}

	hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
				"amoled_ecm", ecm, amoled_ecm_groups);
	if (IS_ERR_OR_NULL(hwmon_dev)) {
		rc = PTR_ERR(hwmon_dev);
		pr_err("failed to register hwmon device for amoled-ecm, rc=%d\n",
				rc);
		return rc;
	}

	return qti_amoled_register_drm_notifier(ecm);
}

static int qti_amoled_ecm_remove(struct platform_device *pdev)
{
	struct amoled_ecm *ecm = dev_get_drvdata(&pdev->dev);

	return qti_amoled_unregister_drm_notifier(ecm);
}

static const struct of_device_id amoled_ecm_match_table[] = {
	{ .compatible = "qcom,amoled-ecm", },
	{ },
};

static struct platform_driver qti_amoled_ecm_driver = {
	.driver = {
		.name = "qti_amoled_ecm",
		.of_match_table = amoled_ecm_match_table,
	},
	.probe = qti_amoled_ecm_probe,
	.remove = qti_amoled_ecm_remove,
};
module_platform_driver(qti_amoled_ecm_driver);

MODULE_DESCRIPTION("QTI AMOLED ECM driver");
MODULE_LICENSE("GPL v2");