aboutsummaryrefslogtreecommitdiff
path: root/drivers/mfd/marimba-codec.c
blob: 2ba251cb4282c84018df2d392e8c0b24c90b6e67 (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
/* Copyright (c) 2009-2010, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * 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.
 *
 */
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/mfd/msm-adie-codec.h>
#include <linux/mfd/marimba.h>
#include <linux/debugfs.h>
#include <linux/uaccess.h>
#include <linux/string.h>

#define MARIMBA_CDC_RX_CTL 0x81
#define MARIMBA_CDC_RX_CTL_ST_EN_MASK 0x20
#define MARIMBA_CDC_RX_CTL_ST_EN_SHFT 0x5
#define MARIMBA_CODEC_CDC_LRXG     0x84
#define MARIMBA_CODEC_CDC_RRXG     0x85
#define MARIMBA_CODEC_CDC_LTXG     0x86
#define MARIMBA_CODEC_CDC_RTXG     0x87

#define MAX_MDELAY_US 2000
#define MIN_MDELAY_US 1000

struct adie_codec_path {
	struct adie_codec_dev_profile *profile;
	struct adie_codec_register_image img;
	u32 hwsetting_idx;
	u32 stage_idx;
	u32 curr_stage;
};

static struct adie_codec_register adie_codec_tx_regs[] = {
	{ 0x04, 0xc0, 0x8C },
	{ 0x0D, 0xFF, 0x00 },
	{ 0x0E, 0xFF, 0x00 },
	{ 0x0F, 0xFF, 0x00 },
	{ 0x10, 0xF8, 0x68 },
	{ 0x11, 0xFE, 0x00 },
	{ 0x12, 0xFE, 0x00 },
	{ 0x13, 0xFF, 0x58 },
	{ 0x14, 0xFF, 0x00 },
	{ 0x15, 0xFE, 0x00 },
	{ 0x16, 0xFF, 0x00 },
	{ 0x1A, 0xFF, 0x00 },
	{ 0x80, 0x01, 0x00 },
	{ 0x82, 0x7F, 0x18 },
	{ 0x83, 0x1C, 0x00 },
	{ 0x86, 0xFF, 0xAC },
	{ 0x87, 0xFF, 0xAC },
	{ 0x89, 0xFF, 0xFF },
	{ 0x8A, 0xF0, 0x30 }
};

static struct adie_codec_register adie_codec_rx_regs[] = {
	{ 0x23, 0xF8, 0x00 },
	{ 0x24, 0x6F, 0x00 },
	{ 0x25, 0x7F, 0x00 },
	{ 0x26, 0xFC, 0x00 },
	{ 0x28, 0xFE, 0x00 },
	{ 0x29, 0xFE, 0x00 },
	{ 0x33, 0xFF, 0x00 },
	{ 0x34, 0xFF, 0x00 },
	{ 0x35, 0xFC, 0x00 },
	{ 0x36, 0xFE, 0x00 },
	{ 0x37, 0xFE, 0x00 },
	{ 0x38, 0xFE, 0x00 },
	{ 0x39, 0xF0, 0x00 },
	{ 0x3A, 0xFF, 0x0A },
	{ 0x3B, 0xFC, 0xAC },
	{ 0x3C, 0xFC, 0xAC },
	{ 0x3D, 0xFF, 0x55 },
	{ 0x3E, 0xFF, 0x55 },
	{ 0x3F, 0xCF, 0x00 },
	{ 0x40, 0x3F, 0x00 },
	{ 0x41, 0x3F, 0x00 },
	{ 0x42, 0xFF, 0x00 },
	{ 0x43, 0xF7, 0x00 },
	{ 0x43, 0xF7, 0x00 },
	{ 0x43, 0xF7, 0x00 },
	{ 0x43, 0xF7, 0x00 },
	{ 0x44, 0xF7, 0x00 },
	{ 0x45, 0xFF, 0x00 },
	{ 0x46, 0xFF, 0x00 },
	{ 0x47, 0xF7, 0x00 },
	{ 0x48, 0xF7, 0x00 },
	{ 0x49, 0xFF, 0x00 },
	{ 0x4A, 0xFF, 0x00 },
	{ 0x80, 0x02, 0x00 },
	{ 0x81, 0xFF, 0x4C },
	{ 0x83, 0x23, 0x00 },
	{ 0x84, 0xFF, 0xAC },
	{ 0x85, 0xFF, 0xAC },
	{ 0x88, 0xFF, 0xFF },
	{ 0x8A, 0x0F, 0x03 },
	{ 0x8B, 0xFF, 0xAC },
	{ 0x8C, 0x03, 0x01 },
	{ 0x8D, 0xFF, 0x00 },
	{ 0x8E, 0xFF, 0x00 }
};

static struct adie_codec_register adie_codec_lb_regs[] = {
	{ 0x2B, 0x8F, 0x02 },
	{ 0x2C, 0x8F, 0x02 }
};

struct adie_codec_state {
	struct adie_codec_path path[ADIE_CODEC_MAX];
	u32 ref_cnt;
	struct marimba *pdrv_ptr;
	struct marimba_codec_platform_data *codec_pdata;
	struct mutex lock;
};

static struct adie_codec_state adie_codec;

/* Array containing write details of Tx and RX Digital Volume
   Tx and Rx and both the left and right channel use the same data
*/
u8 adie_codec_rx_tx_dig_vol_data[] = {
	0x81, 0x82, 0x83, 0x84,
	0x85, 0x86, 0x87, 0x88,
	0x89, 0x8a, 0x8b, 0x8c,
	0x8d, 0x8e, 0x8f, 0x90,
	0x91, 0x92, 0x93, 0x94,
	0x95, 0x96, 0x97, 0x98,
	0x99, 0x9a, 0x9b, 0x9c,
	0x9d, 0x9e, 0x9f, 0xa0,
	0xa1, 0xa2, 0xa3, 0xa4,
	0xa5, 0xa6, 0xa7, 0xa8,
	0xa9, 0xaa, 0xab, 0xac,
	0xad, 0xae, 0xaf, 0xb0,
	0xb1, 0xb2, 0xb3, 0xb4,
	0xb5, 0xb6, 0xb7, 0xb8,
	0xb9, 0xba, 0xbb, 0xbc,
	0xbd, 0xbe, 0xbf, 0xc0,
	0xc1, 0xc2, 0xc3, 0xc4,
	0xc5, 0xc6, 0xc7, 0xc8,
	0xc9, 0xca, 0xcb, 0xcc,
	0xcd, 0xce, 0xcf, 0xd0,
	0xd1, 0xd2, 0xd3, 0xd4,
	0xd5, 0xd6, 0xd7, 0xd8,
	0xd9, 0xda, 0xdb, 0xdc,
	0xdd, 0xde, 0xdf, 0xe0,
	0xe1, 0xe2, 0xe3, 0xe4,
	0xe5, 0xe6, 0xe7, 0xe8,
	0xe9, 0xea, 0xeb, 0xec,
	0xed, 0xee, 0xf0, 0xf1,
	0xf2, 0xf3, 0xf4, 0xf5,
	0xf6, 0xf7, 0xf8, 0xf9,
	0xfa, 0xfb, 0xfc, 0xfd,
	0xfe, 0xff, 0x00, 0x01,
	0x02, 0x03, 0x04, 0x05,
	0x06, 0x07, 0x08, 0x09,
	0x0a, 0x0b, 0x0c, 0x0d,
	0x0e, 0x0f, 0x10, 0x11,
	0x12, 0x13, 0x14, 0x15,
	0x16, 0x17, 0x18, 0x19,
	0x1a, 0x1b, 0x1c, 0x1d,
	0x1e, 0x1f, 0x20, 0x21,
	0x22, 0x23, 0x24, 0x25,
	0x26, 0x27, 0x28, 0x29,
	0x2a, 0x2b, 0x2c, 0x2d,
	0x2e, 0x2f, 0x30, 0x31,
	0x32, 0x33, 0x34, 0x35,
	0x36, 0x37, 0x38, 0x39,
	0x3a, 0x3b, 0x3c, 0x3d,
	0x3e, 0x3f, 0x40, 0x41,
	0x42, 0x43, 0x44, 0x45,
	0x46, 0x47, 0x48, 0x49,
	0x4a, 0x4b, 0x4c, 0x4d,
	0x4e, 0x4f, 0x50, 0x51,
	0x52, 0x53, 0x54, 0x55,
	0x56, 0x57, 0x58, 0x59,
	0x5a, 0x5b, 0x5c, 0x5d,
	0x5e, 0x5f, 0x60, 0x61,
	0x62, 0x63, 0x64, 0x65,
	0x66, 0x67, 0x68, 0x69,
	0x6a, 0x6b, 0x6c, 0x6d,
	0x6e, 0x6f, 0x70, 0x71,
	0x72, 0x73, 0x74, 0x75,
	0x76, 0x77, 0x78, 0x79,
	0x7a, 0x7b, 0x7c, 0x7d,
	0x7e, 0x7f
};

enum adie_vol_type {
	ADIE_CODEC_RX_DIG_VOL,
	ADIE_CODEC_TX_DIG_VOL,
	ADIE_CODEC_VOL_TYPE_MAX
};

struct adie_codec_ch_vol_cntrl {
	u8 codec_reg;
	u8 codec_mask;
	u8 *vol_cntrl_data;
};

struct adie_codec_vol_cntrl_data {

	enum adie_vol_type vol_type;

	/* Jump length used while doing writes in incremental fashion */
	u32 jump_length;
	s32 min_mb;		/* Min Db applicable to the vol control */
	s32 max_mb;		/* Max Db applicable to the vol control */
	u32 step_in_mb;
	u32 steps;		/* No of steps allowed for this vol type */

	struct adie_codec_ch_vol_cntrl *ch_vol_cntrl_info;
};

static struct adie_codec_ch_vol_cntrl adie_codec_rx_vol_cntrl[] = {
	{MARIMBA_CODEC_CDC_LRXG, 0xff, adie_codec_rx_tx_dig_vol_data},
	{MARIMBA_CODEC_CDC_RRXG, 0xff, adie_codec_rx_tx_dig_vol_data}
};

static struct adie_codec_ch_vol_cntrl adie_codec_tx_vol_cntrl[] = {
	{MARIMBA_CODEC_CDC_LTXG, 0xff, adie_codec_rx_tx_dig_vol_data},
	{MARIMBA_CODEC_CDC_RTXG, 0xff, adie_codec_rx_tx_dig_vol_data}
};

static struct adie_codec_vol_cntrl_data adie_codec_vol_cntrl[] = {
	{ADIE_CODEC_RX_DIG_VOL, 5100, -12700, 12700, 100, 255,
	 adie_codec_rx_vol_cntrl},

	{ADIE_CODEC_TX_DIG_VOL, 5100, -12700, 12700, 100, 255,
	 adie_codec_tx_vol_cntrl}
};

static int adie_codec_write(u8 reg, u8 mask, u8 val)
{
	int rc;

	rc = marimba_write_bit_mask(adie_codec.pdrv_ptr, reg,  &val, 1, mask);
	if (IS_ERR_VALUE(rc)) {
		pr_err("%s: fail to write reg %x\n", __func__, reg);
		return -EIO;
	}

	pr_debug("%s: write reg %x val %x\n", __func__, reg, val);

	return 0;
}

static int adie_codec_read(u8 reg, u8 *val)
{
	return marimba_read(adie_codec.pdrv_ptr, reg, val, 1);
}

static int adie_codec_read_dig_vol(enum adie_vol_type vol_type, u32 chan_index,
				   u32 *cur_index)
{
	u32 counter;
	u32 size;
	u8 reg, mask, cur_val;
	int rc;

	reg =
	    adie_codec_vol_cntrl[vol_type].
	    ch_vol_cntrl_info[chan_index].codec_reg;

	mask =
	    adie_codec_vol_cntrl[vol_type].
	    ch_vol_cntrl_info[chan_index].codec_mask;

	rc = marimba_read(adie_codec.pdrv_ptr, reg, &cur_val, 1);

	if (IS_ERR_VALUE(rc)) {
		pr_err("%s: fail to read reg %x\n", __func__, reg);
		return -EIO;
	}

	cur_val = cur_val & mask;

	pr_debug("%s: reg 0x%x  mask 0x%x  reg_value = 0x%x"
		"vol_type = %d\n", __func__, reg, mask, cur_val, vol_type);

	size = adie_codec_vol_cntrl[vol_type].steps;

	for (counter = 0; counter <= size; counter++) {

		if (adie_codec_vol_cntrl[vol_type].ch_vol_cntrl_info
		    [chan_index].vol_cntrl_data[counter] == cur_val) {
			*cur_index = counter;
			return 0;
		}
	}

	pr_err("%s: could not find 0x%x in reg 0x%x values array\n",
			__func__, cur_val, reg);

	return -EINVAL;;
}

static int adie_codec_set_dig_vol(enum adie_vol_type vol_type, u32 chan_index,
				  u32 cur_index, u32 target_index)
{
	u32 count;
	u8 reg, mask, val;
	u32 i;
	u32 index;
	u32 index_jump;

	int rc;

	index_jump = adie_codec_vol_cntrl[vol_type].jump_length;

	reg =
	    adie_codec_vol_cntrl[vol_type].
	    ch_vol_cntrl_info[chan_index].codec_reg;

	mask =
	    adie_codec_vol_cntrl[vol_type].
	    ch_vol_cntrl_info[chan_index].codec_mask;

	/* compare the target index with current index */
	if (cur_index < target_index) {

		/* Volume is being increased loop and increase it in 4-5 steps
		 */
		count = ((target_index - cur_index) * 100 / index_jump);
		index = cur_index;

		for (i = 1; i <= count; i++) {
			index = index + (int)(index_jump / 100);

			val =
			    adie_codec_vol_cntrl[vol_type].ch_vol_cntrl_info
			    [chan_index].vol_cntrl_data[index];

			pr_debug("%s: write reg %x val 0x%x\n",
					__func__, reg, val);

			rc = adie_codec_write(reg, mask, val);
			if (rc < 0) {
				pr_err("%s: write reg %x val 0x%x failed\n",
					__func__, reg, val);
				return rc;
			}
		}

		/*do one final write to take it to the target index level */
		val =
		    adie_codec_vol_cntrl[vol_type].ch_vol_cntrl_info
		    [chan_index].vol_cntrl_data[target_index];

		pr_debug("%s: write reg %x val 0x%x\n", __func__, reg, val);

		rc = adie_codec_write(reg, mask, val);

		if (rc < 0) {
			pr_err("%s: write reg %x val 0x%x failed\n",
					__func__, reg, val);
			return rc;
		}

	} else {

		/* Volume is being decreased from the current setting */
		index = cur_index;
		/* loop and decrease it in 4-5 steps */
		count = ((cur_index - target_index) * 100 / index_jump);

		for (i = 1; i <= count; i++) {
			index = index - (int)(index_jump / 100);

			val =
			    adie_codec_vol_cntrl[vol_type].ch_vol_cntrl_info
			    [chan_index].vol_cntrl_data[index];

			pr_debug("%s: write reg %x val 0x%x\n",
					__func__, reg, val);

			rc = adie_codec_write(reg, mask, val);
			if (rc < 0) {
				pr_err("%s: write reg %x val 0x%x failed\n",
					__func__, reg, val);
				return rc;
			}
		}

		/* do one final write to take it to the target index level */
		val =
		    adie_codec_vol_cntrl[vol_type].ch_vol_cntrl_info
		    [chan_index].vol_cntrl_data[target_index];

		pr_debug("%s: write reg %x val 0x%x\n", __func__, reg, val);

		rc = adie_codec_write(reg, mask, val);

		if (rc < 0) {
			pr_err("%s: write reg %x val 0x%x failed\n",
					__func__, reg, val);
			return rc;
		}
	}
	return 0;
}

static int marimba_adie_codec_set_device_digital_volume(
		struct adie_codec_path *path_ptr,
		u32 num_channels, u32 vol_percentage /* in percentage */)
{
	enum adie_vol_type vol_type;
	s32 milli_bel;
	u32 chan_index;
	u32 step_index;
	u32 cur_step_index = 0;

	if (!path_ptr  || (path_ptr->curr_stage !=
				ADIE_CODEC_DIGITAL_ANALOG_READY)) {
		pr_info("%s: Marimba codec not ready for volume control\n",
		       __func__);
		return  -EPERM;
	}

	if (num_channels > 2) {
		pr_err("%s: Marimba codec only supports max two channels\n",
		       __func__);
		return -EINVAL;
	}

	if (path_ptr->profile->path_type == ADIE_CODEC_RX)
		vol_type = ADIE_CODEC_RX_DIG_VOL;
	else if (path_ptr->profile->path_type == ADIE_CODEC_TX)
		vol_type = ADIE_CODEC_TX_DIG_VOL;
	else {
		pr_err("%s: invalid device data neither RX nor TX\n",
				__func__);
		return -EINVAL;
	}

	milli_bel = ((adie_codec_vol_cntrl[vol_type].max_mb -
			adie_codec_vol_cntrl[vol_type].min_mb) *
			vol_percentage) / 100;

	milli_bel = adie_codec_vol_cntrl[vol_type].min_mb + milli_bel;

	pr_debug("%s: milli bell = %d vol_type = %d vol_percentage = %d"
		 " num_cha =  %d \n",
		 __func__, milli_bel, vol_type, vol_percentage, num_channels);


	step_index = ((milli_bel
		       - adie_codec_vol_cntrl[vol_type].min_mb
		       + (adie_codec_vol_cntrl[vol_type].step_in_mb / 2))
		      / adie_codec_vol_cntrl[vol_type].step_in_mb);


	for (chan_index = 0; chan_index < num_channels; chan_index++) {
		adie_codec_read_dig_vol(vol_type, chan_index, &cur_step_index);

		pr_debug("%s: cur_step_index = %u  current vol = 0x%x\n",
				__func__, cur_step_index,
			adie_codec_vol_cntrl[vol_type].ch_vol_cntrl_info
			[chan_index].vol_cntrl_data[cur_step_index]);

		pr_debug("%s: step index = %u  new volume = 0x%x\n",
		 __func__, step_index,
		 adie_codec_vol_cntrl[vol_type].ch_vol_cntrl_info
		 [chan_index].vol_cntrl_data[step_index]);

		adie_codec_set_dig_vol(vol_type, chan_index, cur_step_index,
				       step_index);

	}
	return 0;
}

static int marimba_adie_codec_setpath(struct adie_codec_path *path_ptr,
					u32 freq_plan, u32 osr)
{
	int rc = 0;
	u32 i, freq_idx = 0, freq = 0;

	if ((path_ptr->curr_stage != ADIE_CODEC_DIGITAL_OFF) &&
		(path_ptr->curr_stage != ADIE_CODEC_FLASH_IMAGE)) {
		rc = -EBUSY;
		goto error;
	}

	for (i = 0; i < path_ptr->profile->setting_sz; i++) {
		if (path_ptr->profile->settings[i].osr == osr) {
			if (path_ptr->profile->settings[i].freq_plan >=
				freq_plan) {
				if (freq == 0) {
					freq = path_ptr->profile->settings[i].
								freq_plan;
					freq_idx = i;
				} else if (path_ptr->profile->settings[i].
					freq_plan < freq) {
					freq = path_ptr->profile->settings[i].
								freq_plan;
					freq_idx = i;
				}
			}
		}
	}

	if (freq_idx >= path_ptr->profile->setting_sz)
		rc = -ENODEV;
	else {
		path_ptr->hwsetting_idx = freq_idx;
		path_ptr->stage_idx = 0;
	}

error:
	return rc;
}

static u32 marimba_adie_codec_freq_supported(
				struct adie_codec_dev_profile *profile,
				u32 requested_freq)
{
	u32 i, rc = -EINVAL;

	for (i = 0; i < profile->setting_sz; i++) {
		if (profile->settings[i].freq_plan >= requested_freq) {
			rc = 0;
			break;
		}
	}
	return rc;
}

static int marimba_adie_codec_enable_sidetone(
				struct adie_codec_path *rx_path_ptr,
				u32 enable)
{
	int rc = 0;

	pr_debug("%s()\n", __func__);

	mutex_lock(&adie_codec.lock);

	if (!rx_path_ptr || &adie_codec.path[ADIE_CODEC_RX] != rx_path_ptr) {
		pr_err("%s: invalid path pointer\n", __func__);
		rc = -EINVAL;
		goto error;
	} else if (rx_path_ptr->curr_stage !=
		ADIE_CODEC_DIGITAL_ANALOG_READY) {
		pr_err("%s: bad state\n", __func__);
		rc = -EPERM;
		goto error;
	}

	if (enable)
		rc = adie_codec_write(MARIMBA_CDC_RX_CTL,
		MARIMBA_CDC_RX_CTL_ST_EN_MASK,
		(0x1 << MARIMBA_CDC_RX_CTL_ST_EN_SHFT));
	else
		rc = adie_codec_write(MARIMBA_CDC_RX_CTL,
		MARIMBA_CDC_RX_CTL_ST_EN_MASK, 0);

error:
	mutex_unlock(&adie_codec.lock);
	return rc;
}

static void adie_codec_reach_stage_action(struct adie_codec_path *path_ptr,
						u32 stage)
{
	u32 iter;
	struct adie_codec_register *reg_info;

	if (stage == ADIE_CODEC_FLASH_IMAGE) {
		/* perform reimage */
		for (iter = 0; iter < path_ptr->img.img_sz; iter++) {
			reg_info = &path_ptr->img.regs[iter];
			adie_codec_write(reg_info->reg,
			reg_info->mask, reg_info->val);
		}
	}
}

static int marimba_adie_codec_proceed_stage(struct adie_codec_path *path_ptr,
						u32 state)
{
	int rc = 0, loop_exit = 0;
	struct adie_codec_action_unit *curr_action;
	struct adie_codec_hwsetting_entry *setting;
	u8 reg, mask, val;

	mutex_lock(&adie_codec.lock);
	setting = &path_ptr->profile->settings[path_ptr->hwsetting_idx];
	while (!loop_exit) {
		curr_action = &setting->actions[path_ptr->stage_idx];
		switch (curr_action->type) {
		case ADIE_CODEC_ACTION_ENTRY:
			ADIE_CODEC_UNPACK_ENTRY(curr_action->action,
			reg, mask, val);
			adie_codec_write(reg, mask, val);
			break;
		case ADIE_CODEC_ACTION_DELAY_WAIT:
			if (curr_action->action > MAX_MDELAY_US)
				msleep(curr_action->action/1000);
			else if (curr_action->action < MIN_MDELAY_US)
				udelay(curr_action->action);
			else
				mdelay(curr_action->action/1000);
			break;
		case ADIE_CODEC_ACTION_STAGE_REACHED:
			adie_codec_reach_stage_action(path_ptr,
				curr_action->action);
			if (curr_action->action == state) {
				path_ptr->curr_stage = state;
				loop_exit = 1;
			}
			break;
		default:
			BUG();
		}

		path_ptr->stage_idx++;
		if (path_ptr->stage_idx == setting->action_sz)
			path_ptr->stage_idx = 0;
	}
	mutex_unlock(&adie_codec.lock);
	return rc;
}

static void marimba_codec_bring_up(void)
{
	/* bring up sequence for Marimba codec core
	 * ensure RESET_N = 0 and GDFS_CLAMP_EN=1 -
	 * set GDFS_EN_FEW=1 then GDFS_EN_REST=1 then
	 * GDFS_CLAMP_EN = 0 and finally RESET_N = 1
	 * Marimba codec bring up should use the Marimba
	 * slave address after which the codec slave
	 * address can be used
	 */

	/* Bring up codec */
	adie_codec_write(0xFF, 0xFF, 0x08);

	/* set GDFS_EN_FEW=1 */
	adie_codec_write(0xFF, 0xFF, 0x0a);

	/* set GDFS_EN_REST=1 */
	adie_codec_write(0xFF, 0xFF, 0x0e);

	/* set RESET_N=1 */
	adie_codec_write(0xFF, 0xFF, 0x07);

	adie_codec_write(0xFF, 0xFF, 0x17);

	/* enable band gap */
	adie_codec_write(0x03, 0xFF, 0x04);

	/* dither delay selected and dmic gain stage bypassed */
	adie_codec_write(0x8F, 0xFF, 0x44);
}

static void marimba_codec_bring_down(void)
{
	adie_codec_write(0xFF, 0xFF, 0x07);
	adie_codec_write(0xFF, 0xFF, 0x06);
	adie_codec_write(0xFF, 0xFF, 0x0e);
	adie_codec_write(0xFF, 0xFF, 0x08);
	adie_codec_write(0x03, 0xFF, 0x00);
}

static int marimba_adie_codec_open(struct adie_codec_dev_profile *profile,
	struct adie_codec_path **path_pptr)
{
	int rc = 0;

	mutex_lock(&adie_codec.lock);

	if (!profile || !path_pptr) {
		rc = -EINVAL;
		goto error;
	}

	if (adie_codec.path[profile->path_type].profile) {
		rc = -EBUSY;
		goto error;
	}

	if (!adie_codec.ref_cnt) {

		if (adie_codec.codec_pdata &&
				adie_codec.codec_pdata->marimba_codec_power) {

			rc = adie_codec.codec_pdata->marimba_codec_power(1);
			if (rc) {
				pr_err("%s: could not power up marimba "
						"codec\n", __func__);
				goto error;
			}
		}
		marimba_codec_bring_up();
	}

	adie_codec.path[profile->path_type].profile = profile;
	*path_pptr = (void *) &adie_codec.path[profile->path_type];
	adie_codec.ref_cnt++;
	adie_codec.path[profile->path_type].hwsetting_idx = 0;
	adie_codec.path[profile->path_type].curr_stage = ADIE_CODEC_FLASH_IMAGE;
	adie_codec.path[profile->path_type].stage_idx = 0;


error:

	mutex_unlock(&adie_codec.lock);
	return rc;
}

static int marimba_adie_codec_close(struct adie_codec_path *path_ptr)
{
	int rc = 0;

	mutex_lock(&adie_codec.lock);

	if (!path_ptr) {
		rc = -EINVAL;
		goto error;
	}
	if (path_ptr->curr_stage != ADIE_CODEC_DIGITAL_OFF)
		adie_codec_proceed_stage(path_ptr, ADIE_CODEC_DIGITAL_OFF);

	BUG_ON(!adie_codec.ref_cnt);

	path_ptr->profile = NULL;
	adie_codec.ref_cnt--;

	if (!adie_codec.ref_cnt) {

		marimba_codec_bring_down();

		if (adie_codec.codec_pdata &&
				adie_codec.codec_pdata->marimba_codec_power) {

			rc = adie_codec.codec_pdata->marimba_codec_power(0);
			if (rc) {
				pr_err("%s: could not power down marimba "
						"codec\n", __func__);
				goto error;
			}
		}
	}

error:
	mutex_unlock(&adie_codec.lock);
	return rc;
}

static const struct adie_codec_operations marimba_adie_ops = {
	.codec_id = MARIMBA_ID,
	.codec_open = marimba_adie_codec_open,
	.codec_close = marimba_adie_codec_close,
	.codec_setpath = marimba_adie_codec_setpath,
	.codec_proceed_stage = marimba_adie_codec_proceed_stage,
	.codec_freq_supported = marimba_adie_codec_freq_supported,
	.codec_enable_sidetone = marimba_adie_codec_enable_sidetone,
	.codec_set_device_digital_volume =
			marimba_adie_codec_set_device_digital_volume,
};

#ifdef CONFIG_DEBUG_FS
static struct dentry *debugfs_marimba_dent;
static struct dentry *debugfs_peek;
static struct dentry *debugfs_poke;
static struct dentry *debugfs_power;

static unsigned char read_data;

static int codec_debug_open(struct inode *inode, struct file *file)
{
	file->private_data = inode->i_private;
	return 0;
}

static int get_parameters(char *buf, long int *param1, int num_of_par)
{
	char *token;
	int base, cnt;

	token = strsep(&buf, " ");

	for (cnt = 0; cnt < num_of_par; cnt++) {
		if (token != NULL) {
			if ((token[1] == 'x') || (token[1] == 'X'))
				base = 16;
			else
				base = 10;

			if (strict_strtoul(token, base, &param1[cnt]) != 0)
				return -EINVAL;

			token = strsep(&buf, " ");
			}
		else
			return -EINVAL;
	}
	return 0;
}

static ssize_t codec_debug_read(struct file *file, char __user *ubuf,
				size_t count, loff_t *ppos)
{
	char lbuf[8];

	snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
	return simple_read_from_buffer(ubuf, count, ppos, lbuf, strlen(lbuf));
}

static ssize_t codec_debug_write(struct file *filp,
	const char __user *ubuf, size_t cnt, loff_t *ppos)
{
	char *access_str = filp->private_data;
	char lbuf[32];
	int rc;
	long int param[5];

	if (cnt > sizeof(lbuf) - 1)
		return -EINVAL;

	rc = copy_from_user(lbuf, ubuf, cnt);
	if (rc)
		return -EFAULT;

	lbuf[cnt] = '\0';

	if (!strcmp(access_str, "power")) {
		if (get_parameters(lbuf, param, 1) == 0) {
			switch (param[0]) {
			case 1:
				adie_codec.codec_pdata->marimba_codec_power(1);
				marimba_codec_bring_up();
				break;
			case 0:
				marimba_codec_bring_down();
				adie_codec.codec_pdata->marimba_codec_power(0);
				break;
			default:
				rc = -EINVAL;
				break;
			}
		} else
			rc = -EINVAL;
	} else if (!strcmp(access_str, "poke")) {
		/* write */
		rc = get_parameters(lbuf, param, 2);
		if ((param[0] <= 0xFF) && (param[1] <= 0xFF) &&
			(rc == 0))
			adie_codec_write(param[0], 0xFF, param[1]);
		else
			rc = -EINVAL;
	} else if (!strcmp(access_str, "peek")) {
		/* read */
		rc = get_parameters(lbuf, param, 1);
		if ((param[0] <= 0xFF) && (rc == 0))
			adie_codec_read(param[0], &read_data);
		else
			rc = -EINVAL;
	}

	if (rc == 0)
		rc = cnt;
	else
		pr_err("%s: rc = %d\n", __func__, rc);

	return rc;
}

static const struct file_operations codec_debug_ops = {
	.open = codec_debug_open,
	.write = codec_debug_write,
	.read = codec_debug_read
};
#endif

static int marimba_codec_probe(struct platform_device *pdev)
{
	int rc;

	adie_codec.pdrv_ptr = platform_get_drvdata(pdev);
	adie_codec.codec_pdata = pdev->dev.platform_data;

	if (adie_codec.codec_pdata->snddev_profile_init)
		adie_codec.codec_pdata->snddev_profile_init();

	/* Register the marimba ADIE operations */
	rc = adie_codec_register_codec_operations(&marimba_adie_ops);

#ifdef CONFIG_DEBUG_FS
	debugfs_marimba_dent = debugfs_create_dir("msm_adie_codec", 0);
	if (!IS_ERR(debugfs_marimba_dent)) {
		debugfs_peek = debugfs_create_file("peek",
		S_IFREG | S_IRUGO, debugfs_marimba_dent,
		(void *) "peek", &codec_debug_ops);

		debugfs_poke = debugfs_create_file("poke",
		S_IFREG | S_IRUGO, debugfs_marimba_dent,
		(void *) "poke", &codec_debug_ops);

		debugfs_power = debugfs_create_file("power",
		S_IFREG | S_IRUGO, debugfs_marimba_dent,
		(void *) "power", &codec_debug_ops);
	}
#endif
	return rc;
}

static struct platform_driver marimba_codec_driver = {
	.probe = marimba_codec_probe,
	.driver = {
		.name = "marimba_codec",
		.owner = THIS_MODULE,
	},
};

static int __init marimba_codec_init(void)
{
	s32 rc;

	rc = platform_driver_register(&marimba_codec_driver);
	if (IS_ERR_VALUE(rc))
		goto error;

	adie_codec.path[ADIE_CODEC_TX].img.regs = adie_codec_tx_regs;
	adie_codec.path[ADIE_CODEC_TX].img.img_sz =
	ARRAY_SIZE(adie_codec_tx_regs);
	adie_codec.path[ADIE_CODEC_RX].img.regs = adie_codec_rx_regs;
	adie_codec.path[ADIE_CODEC_RX].img.img_sz =
	ARRAY_SIZE(adie_codec_rx_regs);
	adie_codec.path[ADIE_CODEC_LB].img.regs = adie_codec_lb_regs;
	adie_codec.path[ADIE_CODEC_LB].img.img_sz =
	ARRAY_SIZE(adie_codec_lb_regs);
	mutex_init(&adie_codec.lock);

error:
	return rc;
}

static void __exit marimba_codec_exit(void)
{
#ifdef CONFIG_DEBUG_FS
	debugfs_remove(debugfs_peek);
	debugfs_remove(debugfs_poke);
	debugfs_remove(debugfs_power);
	debugfs_remove(debugfs_marimba_dent);
#endif
	platform_driver_unregister(&marimba_codec_driver);
}

module_init(marimba_codec_init);
module_exit(marimba_codec_exit);

MODULE_DESCRIPTION("Marimba codec driver");
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL v2");