aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/samsung/fm_si47xx/Si47xx_dev.c
blob: bf6491526dc8affa655357ea17819fa7f107a25f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
/* drivers/samsung/fm_si47xx/Si47xx_dev.c
 *
 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/kernel.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/delay.h>
#include <linux/time.h>
#include <linux/workqueue.h>
#include <linux/interrupt.h>

#include <mach/gpio.h>
#include <plat/gpio-cfg.h>

#include "Si47xx_dev.h"
#include <linux/i2c/si47xx_common.h>

#include "commanddefs.h"
#include "propertydefs.h"


enum {
	eTRUE,
	eFALSE,
} dev_struct_status_t;

/*dev_state*/
/*power_state*/
#define RADIO_ON		1
#define RADIO_POWERDOWN		0
/*seek_state*/
#define RADIO_SEEK_ON		1
#define RADIO_SEEK_OFF		0

#define FREQ_87500_kHz		8750
#define FREQ_76000_kHz		7600

#define TUNE_RSSI_THRESHOLD	10
#define TUNE_SNR_THRESHOLD	4
#define TUNE_CNT_THRESHOLD	0x00

#define _ENABLE_RDS_

#ifdef RDS_INTERRUPT_ON_ALWAYS
#define RDS_BUFFER_LENGTH	50
static u16 *RDS_Block_Data_buffer;
static u8 *RDS_Block_Error_buffer;
static u8 RDS_Buffer_Index_read;	/* index number for last read data */
static u8 RDS_Buffer_Index_write;	/* index number for last written data */

int Si47xx_RDS_flag;
int RDS_Data_Available;
int RDS_Data_Lost;
int RDS_Groups_Available_till_now;
struct workqueue_struct *Si47xx_wq;
struct work_struct Si47xx_work;
#endif

/*Si47xx device structure*/
static struct Si47xx_device_t *Si47xx_dev;
static struct si47xx_platform_data *pSi47xxdata;

static int si47xx_irq;
wait_queue_head_t Si47xx_waitq;

/*Wait flag*/
/*WAITING or WAIT_OVER or NO_WAIT*/
int Si47xx_dev_wait_flag = NO_WAIT;
#ifdef RDS_INTERRUPT_ON_ALWAYS
int Si47xx_RDS_flag = NO_WAIT;
#endif

static irqreturn_t Si47xx_isr(int irq, void *unused)
{
	debug("Si47xx_isr: FM device called IRQ: %d\n", irq);
#ifdef RDS_INTERRUPT_ON_ALWAYS
	if ((Si47xx_dev_wait_flag == SEEK_WAITING) ||
	    (Si47xx_dev_wait_flag == TUNE_WAITING)) {
		debug("Si47xx_isr: FM Seek/Tune Interrupt "
			"called IRQ %d\n", irq);
		Si47xx_dev_wait_flag = WAIT_OVER;
		wake_up_interruptible(&Si47xx_waitq);
	} else if (Si47xx_RDS_flag == RDS_WAITING) {	/* RDS Interrupt */
		debug_rds("Si47xx_isr: FM RDS Interrupt "
			"called IRQ %d", irq);

		debug_rds("RDS_Groups_Available_till_now b/w "
			"Power ON/OFF : %d",
			  RDS_Groups_Available_till_now);

		if (!work_pending(&Si47xx_work))
			queue_work(Si47xx_wq, &Si47xx_work);
	}
#else
	if ((Si47xx_dev_wait_flag == SEEK_WAITING) ||
	    (Si47xx_dev_wait_flag == TUNE_WAITING) ||
	    (Si47xx_dev_wait_flag == RDS_WAITING)) {
		Si47xx_dev_wait_flag = WAIT_OVER;
		wake_up_interruptible(&Si47xx_waitq);
	}
#endif
	return IRQ_HANDLED;
}


/*-----------------------------------------------------------------------------
 This command returns the status
-----------------------------------------------------------------------------*/
static u8 si47xx_readStatus(void)
{
	u8 status;
	int ret = 0;
	ret = i2c_master_recv((struct i2c_client *)(Si47xx_dev->client),
		&status, 1);

	if (ret < 0) {
		dev_err(Si47xx_dev->dev, "%s si47xx_readStatus failed %d\n",
		__func__, ret);

		return ret;
	}

	return status;
}


/*-----------------------------------------------------------------------------
 Command that will wait for CTS before returning
-----------------------------------------------------------------------------*/
static void si47xx_waitForCTS(void)
{
	u16 i = 1000;
	u8 rStatus = 0;

	do {
		rStatus = si47xx_readStatus();
		usleep_range(5, 10);
	} while (--i && !(rStatus & CTS));
}

/*-----------------------------------------------------------------------------
 Sends a command to the part and returns the reply bytes
-----------------------------------------------------------------------------*/
static int si47xx_command(u8 cmd_size, u8 *cmd, u8 reply_size, u8 *reply)
{
	int ret = 0;
	ret = i2c_master_send((struct i2c_client *)(Si47xx_dev->client),
		cmd, cmd_size);
	if (ret < 0) {
		dev_err(Si47xx_dev->dev, "%s si47xx_command failed %d\n",
			__func__, ret);

		return ret;
	}
	si47xx_waitForCTS();

	if (reply_size) {
		ret = i2c_master_recv((struct i2c_client *)(Si47xx_dev->client),
		reply, reply_size);

		if (ret < 0)
			dev_err(Si47xx_dev->dev,
			"%s si47xx_command failed %d\n", __func__, ret);

		return ret;
	}

	return ret;
}

/*-----------------------------------------------------------------------------
 Set the passed property number to the passed value.

 Inputs:
      propNumber:  The number identifying the property to set
      propValue:   The value of the property.
-----------------------------------------------------------------------------*/
static void si47xx_set_property(u16 propNumber, u16 propValue)
{
	u8 cmd[8];
	int ret = 0;

	cmd[0] = SET_PROPERTY;
	cmd[1] = 0;
	cmd[2] = (u8)(propNumber >> 8);
	cmd[3] = (u8)(propNumber & 0x00FF);
	cmd[4] = (u8)(propValue >> 8);
	cmd[5] = (u8)(propValue & 0x00FF);

	ret = si47xx_command(6, cmd, 0, NULL);

	if (ret < 0)
		dev_err(Si47xx_dev->dev,
		"%s si47xx_set_property failed %d\n", __func__, ret);
}

static int powerup(void)
{
	int ret = 0;
	u8 cmd[8];
	u8 rsp[13];

	pSi47xxdata->power(1);

	cmd[0] = POWER_UP;
	cmd[1] = POWER_UP_IN_GPO2OEN | POWER_UP_IN_FUNC_FMRX;
	cmd[2] = POWER_UP_IN_OPMODE_RX_ANALOG;
	ret = si47xx_command(3, cmd, 8, rsp);

	if (ret < 0) {
		dev_err(Si47xx_dev->dev, "%s failed %d\n", __func__, ret);
	} else {
		/* Si4709/09 datasheet: Table 7 */
		msleep(110);
		Si47xx_dev->state.power_state = RADIO_ON;
	}
	return ret;
}

static int powerdown(void)
{
	int ret = 0;
	u8 cmd[8];
	u8 rsp[13];

	if (!(RADIO_POWERDOWN == Si47xx_dev->state.power_state)) {
		cmd[0] = POWER_DOWN;
		ret = si47xx_command(1, cmd, 1, rsp);

		if (ret < 0)
			dev_err(Si47xx_dev->dev, "%s failed %d\n",
			__func__, ret);
		else
			Si47xx_dev->state.power_state = RADIO_POWERDOWN;

		msleep(110);
		pSi47xxdata->power(0);
	} else
		debug("Device already Powered-OFF\n");

	return ret;
}

/*-----------------------------------------------------------------------------
 Helper function that sends the GET_INT_STATUS command to the part

 Returns:
   The status byte from the part.
-----------------------------------------------------------------------------*/
static s8 getIntStatus(void)
{
	u8 cmd[8];
	u8 rsp[13];
	int ret = 0;
	cmd[0] = GET_INT_STATUS;
	ret = si47xx_command(1, cmd, 1, rsp);

	if (ret < 0) {
		dev_err(Si47xx_dev->dev, "%s getIntStatus failed %d\n",
				__func__, ret);
		return ret;
	}
	return rsp[0];
}

/*-----------------------------------------------------------------------------
 Helper function that sends the FM_SEEK_START command to the part

Inputs:
seekUp: If non-zero seek will increment otherwise decrement
wrap:   If non-zero seek will wrap around band limits when hitting the end
of the band limit.
-----------------------------------------------------------------------------*/
static int fmSeekStart(u8 seekUp, u8 wrap)
{
	u8 cmd[8];
	u8 rsp[13];
	int ret;

	cmd[0] = FM_SEEK_START;
	cmd[1] = 0;
	if (seekUp)
		cmd[1] |= FM_SEEK_START_IN_SEEKUP;
	if (wrap)
		cmd[1] |= FM_SEEK_START_IN_WRAP;

	ret = si47xx_command(2, cmd, 1, rsp);
	return ret;

}

static u16 freq_to_channel(u32 frequency)
{
	u16 channel;

	if (frequency < Si47xx_dev->settings.bottom_of_band)
		frequency = Si47xx_dev->settings.bottom_of_band;

	channel = (frequency - Si47xx_dev->settings.bottom_of_band)
		/ Si47xx_dev->settings.channel_spacing;

	return channel;
}

/* Only one thread will be able to call this, since this function call is
   protected by a mutex, so no race conditions can arise */
static void wait(void)
{
	wait_event_interruptible(Si47xx_waitq,
			(Si47xx_dev_wait_flag == WAIT_OVER) ||
			(Si47xx_dev_wait_flag == SEEK_CANCEL));
}

#ifndef RDS_INTERRUPT_ON_ALWAYS
static void wait_RDS(void)
{
	wait_event_interruptible_timeout(Si47xx_waitq,
			(Si47xx_dev_wait_flag == WAIT_OVER),
			Si47xx_dev->settings.timeout_RDS);
}
#endif

/*-----------------------------------------------------------------------------
 Helper function that sends the FM_TUNE_FREQ command to the part

 Inputs:
 frequency in 10kHz steps
-----------------------------------------------------------------------------*/
static int fmTuneFreq(u16 frequency)
{
	u8 cmd[8];
	u8 rsp[13];
	int ret;

	cmd[0] = FM_TUNE_FREQ;
	cmd[1] = 0;
	cmd[2] = (u8)(frequency >> 8);
	cmd[3] = (u8)(frequency & 0x00FF);
	cmd[4] = (u8)0;
	ret  = si47xx_command(5, cmd, 1, rsp);

	return ret;
}

/*-----------------------------------------------------------------------------
 Helper function that sends the FM_TUNE_STATUS command to the part

 Inputs:
 cancel: If non-zero the current seek will be cancelled.
 intack: If non-zero the interrupt for STCINT will be cleared.

Outputs:  These are global variables and are set by this method
STC:    The seek/tune is complete
BLTF:   The seek reached the band limit or original start frequency
AFCRL:  The AFC is railed if this is non-zero
Valid:  The station is valid if this is non-zero
Freq:   The current frequency
RSSI:   The RSSI level read at tune.
ASNR:   The audio SNR level read at tune.
AntCap: The current level of the tuning capacitor.
-----------------------------------------------------------------------------*/

static int fmTuneStatus(u8 cancel, u8 intack, struct tune_data_t *tune_data)
{
	u8 cmd[8];
	u8 rsp[13];
	int ret;

	cmd[0] = FM_TUNE_STATUS;
	cmd[1] = 0;
	if (cancel)
		cmd[1] |= FM_TUNE_STATUS_IN_CANCEL;
	if (intack)
		cmd[1] |= FM_TUNE_STATUS_IN_INTACK;

	ret = si47xx_command(2, cmd, 8, rsp);

	tune_data->stc    = !!(rsp[0] & STCINT);
	tune_data->bltf   = !!(rsp[1] & FM_TUNE_STATUS_OUT_BTLF);
	tune_data->afcrl  = !!(rsp[1] & FM_TUNE_STATUS_OUT_AFCRL);
	tune_data->valid  = !!(rsp[1] & FM_TUNE_STATUS_OUT_VALID);
	tune_data->freq   = ((u16)rsp[2] << 8) | (u16)rsp[3];
	tune_data->rssi   = rsp[4];
	tune_data->asnr   = rsp[5];
	tune_data->antcap = rsp[7];

	return ret;
}

/* -----------------------------------------------------------------------------
 Helper function that sends the FM_RSQ_STATUS command to the part

 Inputs:
  intack: If non-zero the interrupt for STCINT will be cleared.

  Outputs:
  Si47xx_status.Status:  Contains bits about the status returned from the part.
  Si47xx_status.RsqInts: Contains bits about the interrupts
  that have fired related to RSQ.
  SMUTE:   The soft mute function is currently enabled
  AFCRL:   The AFC is railed if this is non-zero
  Valid:   The station is valid if this is non-zero
  Pilot:   A pilot tone is currently present
  Blend:   Percentage of blend for stereo. (100 = full stereo)
  RSSI:    The RSSI level read at tune.
  ASNR:    The audio SNR level read at tune.
  FreqOff: The frequency offset in kHz of the current station
  from the tuned frequency.
-----------------------------------------------------------------------------*/
static void fmRsqStatus(u8 intack, struct rsq_data_t *rsq_data)
{
	u8 cmd[8];
	u8 rsp[13];

	cmd[0] = FM_RSQ_STATUS;
	cmd[1] = 0;

	if (intack)
		cmd[1] |= FM_RSQ_STATUS_IN_INTACK;

	si47xx_command(2, cmd, 8, rsp);

	rsq_data->rsqints = rsp[1];
	rsq_data->smute   = !!(rsp[2] & FM_RSQ_STATUS_OUT_SMUTE);
	rsq_data->afcrl   = !!(rsp[2] & FM_RSQ_STATUS_OUT_AFCRL);
	rsq_data->valid   = !!(rsp[2] & FM_RSQ_STATUS_OUT_VALID);
	rsq_data->pilot   = !!(rsp[3] & FM_RSQ_STATUS_OUT_PILOT);
	rsq_data->blend   = rsp[3] & FM_RSQ_STATUS_OUT_STBLEND;
	rsq_data->rssi    = rsp[4];
	rsq_data->snr    = rsp[5];
	rsq_data->freqoff = rsp[7];
}

/*-----------------------------------------------------------------------------
 Helper function that sends the FM_RDS_STATUS command to the part

 Inputs:
  intack: If non-zero the interrupt for STCINT will be cleared.
  mtfifo: If non-zero the fifo will be cleared.

Outputs:
Status:      Contains bits about the status returned from the part.
RdsInts:     Contains bits about the interrupts that have fired
related to RDS.
RdsSync:     If non-zero the RDS is currently synced.
GrpLost:     If non-zero some RDS groups were lost.
RdsFifoUsed: The amount of groups currently remaining
in the RDS fifo.
BlockA:      Block A group data from the oldest FIFO entry.
BlockB:      Block B group data from the oldest FIFO entry.
BlockC:      Block C group data from the oldest FIFO entry.
BlockD:      Block D group data from the oldest FIFO entry.
BleA:        Block A corrected error information.
BleB:        Block B corrected error information.
BleC:        Block C corrected error information.
BleD:        Block D corrected error information.
-----------------------------------------------------------------------------*/
static void fmRdsStatus(u8 intack, u8 mtfifo, struct radio_data_t *rds_data,
				u8 *RdsFifoUsed)
{
	u8 cmd[8];
	u8 rsp[13];
	int ret = 0;

	cmd[0] = FM_RDS_STATUS;
	cmd[1] = 0;
	if (intack)
		cmd[1] |= FM_RDS_STATUS_IN_INTACK;
	if (mtfifo)
		cmd[1] |= FM_RDS_STATUS_IN_MTFIFO;

	ret = si47xx_command(2, cmd, 13, rsp);
	if (ret < 0) {
		dev_err(Si47xx_dev->dev, "%s fmRdsStatusfailed %d\n",
				__func__, ret);
		return;
	}

	*RdsFifoUsed = rsp[3];
	rds_data->rdsa      = ((u16)rsp[4] << 8) | (u16)rsp[5];
	rds_data->rdsb      = ((u16)rsp[6] << 8) | (u16)rsp[7];
	rds_data->rdsc      = ((u16)rsp[8] << 8) | (u16)rsp[9];
	rds_data->rdsd      = ((u16)rsp[10] << 8) | (u16)rsp[11];
	rds_data->blera = (rsp[12] & FM_RDS_STATUS_OUT_BLEA) >>
		FM_RDS_STATUS_OUT_BLEA_SHFT;
	rds_data->blerb        = (rsp[12] & FM_RDS_STATUS_OUT_BLEB) >>
		FM_RDS_STATUS_OUT_BLEB_SHFT;
	rds_data->blerc        = (rsp[12] & FM_RDS_STATUS_OUT_BLEC) >>
		FM_RDS_STATUS_OUT_BLEC_SHFT;
	rds_data->blerd        = (rsp[12] & FM_RDS_STATUS_OUT_BLED) >>
		FM_RDS_STATUS_OUT_BLED_SHFT;
}

static int seek(u32 *frequency, int up, int mode)
{
	int ret = 0;
	struct tune_data_t tune_data;

	Si47xx_dev_wait_flag = SEEK_WAITING;
	ret = fmSeekStart(up, mode); /* mode 0 is full scan */
	wait();

	if (Si47xx_dev_wait_flag == SEEK_CANCEL) {
		ret = fmTuneStatus(1, 1, &tune_data);

		*frequency = 0;

		Si47xx_dev_wait_flag = NO_WAIT;

		return ret;
	}

	Si47xx_dev_wait_flag = NO_WAIT;

	if (!(getIntStatus() & STCINT)) {
		dev_err(Si47xx_dev->dev, "%s seek is failed!\n", __func__);
		fmTuneStatus(1, 1, &tune_data);
		return -1;
	}

	ret = fmTuneStatus(0, 1, &tune_data);
	if (tune_data.bltf != 1)
		*frequency = tune_data.freq;

	else {
		if (tune_data.valid)
			*frequency = tune_data.freq;
		else
			*frequency = 0;
	}
	return ret;
}

static int tune_freq(u32 frequency)
{
	int ret = 0;

	u16 channel = 0;
	struct tune_data_t tune_data;
	mutex_lock(&(Si47xx_dev->lock));

	channel = freq_to_channel(frequency);

	Si47xx_dev_wait_flag = TUNE_WAITING;
	ret = fmTuneFreq(frequency);
	wait();
	Si47xx_dev_wait_flag = NO_WAIT;
	debug("Si47xx_dev_wait_flag = TUNE_WAITING\n");

	if (!(getIntStatus() & STCINT)) {
		dev_err(Si47xx_dev->dev, "%s tune is failed!\n", __func__);
		fmTuneStatus(1, 1, &tune_data);
		mutex_unlock(&(Si47xx_dev->lock));
		return -1;
	}

	ret = fmTuneStatus(0, 1, &tune_data);

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}


int Si47xx_dev_init(struct Si47xx_device_t *si47xx_dev)
{
	int ret = 0;
	Si47xx_dev = si47xx_dev;
	Si47xx_dev->client = si47xx_dev->client;
	pSi47xxdata = si47xx_dev->pdata;
	si47xx_irq = Si47xx_dev->client->irq;

	debug("Si47xx_dev_init called");

	mutex_lock(&Si47xx_dev->lock);

	Si47xx_dev->state.power_state = RADIO_POWERDOWN;
	Si47xx_dev->state.seek_state = RADIO_SEEK_OFF;
	Si47xx_dev->valid_client_state = eTRUE;
	Si47xx_dev->valid = eFALSE;

#ifdef RDS_INTERRUPT_ON_ALWAYS
	/*Creating Circular Buffer */
	/*Single RDS_Block_Data buffer size is 4x16 bits */
	RDS_Block_Data_buffer = kzalloc(RDS_BUFFER_LENGTH * 8, GFP_KERNEL);
	if (!RDS_Block_Data_buffer) {
		dev_err(Si47xx_dev->dev, "Not sufficient memory for creating "
				"RDS_Block_Data_buffer");
		ret = -ENOMEM;
		goto EXIT;
	}

	/*Single RDS_Block_Error buffer size is 4x8 bits */
	RDS_Block_Error_buffer = kzalloc(RDS_BUFFER_LENGTH * 4, GFP_KERNEL);
	if (!RDS_Block_Error_buffer) {
		dev_err(Si47xx_dev->dev, "Not sufficient memory for creating "
				"RDS_Block_Error_buffer");
		ret = -ENOMEM;
		kfree(RDS_Block_Data_buffer);
		goto EXIT;
	}

	/*Initialising read and write indices */
	RDS_Buffer_Index_read = 0;
	RDS_Buffer_Index_write = 0;

	/*Creating work-queue */
	Si47xx_wq = create_singlethread_workqueue("Si47xx_wq");
	if (!Si47xx_wq) {
		dev_err(Si47xx_dev->dev, "Not sufficient memory for Si47xx_wq, work-queue");
		ret = -ENOMEM;
		kfree(RDS_Block_Error_buffer);
		kfree(RDS_Block_Data_buffer);
		goto EXIT;
	}

	/*Initialising work_queue */
	INIT_WORK(&Si47xx_work, Si47xx_work_func);

	RDS_Data_Available = 0;
	RDS_Data_Lost = 0;
	RDS_Groups_Available_till_now = 0;
EXIT:
#endif

	mutex_unlock(&(Si47xx_dev->lock));

	debug("Si47xx_dev_init call over");

	return ret;
}

int Si47xx_dev_exit(void)
{
	int ret = 0;

	debug("Si47xx_dev_exit called");

	mutex_lock(&(Si47xx_dev->lock));
#ifdef RDS_INTERRUPT_ON_ALWAYS
	if (Si47xx_wq)
		destroy_workqueue(Si47xx_wq);

	kfree(RDS_Block_Error_buffer);
	kfree(RDS_Block_Data_buffer);
#endif

	mutex_unlock(&(Si47xx_dev->lock));

	debug("Si47xx_dev_exit call over");

	return ret;
}

int Si47xx_dev_powerup(void)
{
	int ret = 0;
	u32 value = 100;

	debug("Si47xx_dev_powerup called");

	if (!(RADIO_ON == Si47xx_dev->state.power_state)) {
		ret = powerup();
		if (ret < 0) {
			dev_err(Si47xx_dev->dev, "%s failed %d\n",
				__func__, ret);
		} else if (Si47xx_dev->valid_client_state == eFALSE) {
			dev_err(Si47xx_dev->dev, "Si47xx_dev_powerup called "
					"when DS(state, client) is invalid");
			ret = -1;
		} else {
/* initial settings */
#ifdef _ENABLE_RDS_
			si47xx_set_property(FM_RDS_CONFIG, 1);
			si47xx_set_property(GPO_IEN, GPO_IEN_STCIEN_MASK |
				GPO_IEN_STCREP_MASK);
			si47xx_set_property(GPO_IEN, GPO_IEN_STCIEN_MASK |
				GPO_IEN_RDSIEN_MASK | GPO_IEN_STCREP_MASK);
			si47xx_set_property(FM_RDS_INTERRUPT_SOURCE,
				FM_RDS_INTERRUPT_SOURCE_RECV_MASK);
			si47xx_set_property(FM_RDS_CONFIG,
				FM_RDS_CONFIG_RDSEN_MASK |
				(3 << FM_RDS_CONFIG_BLETHA_SHFT) |
				(3 << FM_RDS_CONFIG_BLETHB_SHFT) |
				(3 << FM_RDS_CONFIG_BLETHC_SHFT) |
				(3 << FM_RDS_CONFIG_BLETHD_SHFT));
#endif
/*VNVS:18-NOV'09 : Setting DE-Time Constant as 50us(Europe,Japan,Australia)*/
			si47xx_set_property(FM_DEEMPHASIS, FM_DEEMPH_50US);
			/* SYSCONFIG2_BITSET_SEEKTH( */
			/*      &Si47xx_dev->registers[SYSCONFIG2],2); */
/*VNVS:18-NOV'09 : modified for detecting more stations of good quality*/
			si47xx_set_property(FM_SEEK_TUNE_RSSI_THRESHOLD,
				TUNE_RSSI_THRESHOLD);
			si47xx_set_property(FM_SEEK_BAND_BOTTOM, 8750);
			si47xx_set_property(FM_SEEK_BAND_TOP, 10800);
			Si47xx_dev->settings.band = BAND_87500_108000_kHz;
			Si47xx_dev->settings.bottom_of_band = FREQ_87500_kHz;
			si47xx_set_property(FM_SEEK_FREQ_SPACING,
				CHAN_SPACING_100_kHz);
			Si47xx_dev->settings.channel_spacing =
				CHAN_SPACING_100_kHz;

			/* SYSCONFIG3_BITSET_SKSNR( */
			/*      &Si47xx_dev->registers[SYSCONFIG3],3); */
/*VNVS:18-NOV'09 : modified for detecting more stations of good quality*/
			si47xx_set_property(FM_SEEK_TUNE_SNR_THRESHOLD,
			TUNE_SNR_THRESHOLD);
			Si47xx_dev->settings.timeout_RDS =
				msecs_to_jiffies(value);
			Si47xx_dev->settings.curr_snr = TUNE_SNR_THRESHOLD;
			Si47xx_dev->settings.curr_rssi_th = TUNE_RSSI_THRESHOLD;
			Si47xx_dev->valid = eTRUE;

			Si47xx_dev_STEREO_SET();
#ifdef RDS_INTERRUPT_ON_ALWAYS
/*Initialising read and write indices */
			RDS_Buffer_Index_read = 0;
			RDS_Buffer_Index_write = 0;

			RDS_Data_Available = 0;
			RDS_Data_Lost = 0;
			RDS_Groups_Available_till_now = 0;
#endif

		}
	} else
		debug("Device already Powered-ON");

	ret = request_irq(si47xx_irq, Si47xx_isr,
		IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "Si47xx", NULL);
	si47xx_set_property(0xff00, 0);

	/* tune initial frequency to remove tunestatus func err
	 * sometimes occur tunestatus func err when execute tunestatus function
	 * before to complete tune_freq.
	 * so run tune_freq just after to complete booting sequence*/
	ret = tune_freq(Si47xx_dev->settings.bottom_of_band);

	return ret;
}

int Si47xx_dev_powerdown(void)
{
	int ret = 0;

	msleep(500);		/* For avoiding turned off pop noise */
	debug("Si47xx_dev_powerdown called");

	mutex_lock(&(Si47xx_dev->lock));

	free_irq(si47xx_irq, NULL);

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_powerdown called when DS is invalid");
		ret = -1;
	} else {
		ret = powerdown();
		if (ret < 0)
			dev_err(Si47xx_dev->dev, "%s failed %d\n",
			__func__, ret);
	}
	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_suspend(void)
{
	int ret = 0;

	debug("Si47xx_dev_suspend called");

#ifndef _ENABLE_RDS_
	disable_irq(si47xx_irq);
#endif

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid_client_state == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_suspend called "
				"when DS(state, client) is invalid");
		ret = -1;
	}
	mutex_unlock(&(Si47xx_dev->lock));

	debug("Si47xx_dev_enable call over");

	return ret;
}

int Si47xx_dev_resume(void)
{
	int ret = 0;

	debug("Si47xx_dev_resume called");

#ifndef _ENABLE_RDS_
	enable_irq(si47xx_irq);
#endif

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid_client_state == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_resume called "
				"when DS(state, client) is invalid");
		ret = -1;
	}

	mutex_unlock(&(Si47xx_dev->lock));
	debug("Si47xx_dev_disable call over");

	return ret;
}

int Si47xx_dev_band_set(int band)
{
	int ret = 0;
	u16 prev_band = 0;
	u32 prev_bottom_of_band = 0;

	debug("Si47xx_dev_band_set called");

	prev_band = Si47xx_dev->settings.band;
	prev_bottom_of_band = Si47xx_dev->settings.bottom_of_band;
	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_band_set called when DS is invalid");
		ret = -1;
	} else {
		switch (band) {
		case BAND_87500_108000_kHz:
			si47xx_set_property(FM_SEEK_BAND_BOTTOM, 8750);
			si47xx_set_property(FM_SEEK_BAND_TOP, 10800);
			Si47xx_dev->settings.band = BAND_87500_108000_kHz;
			Si47xx_dev->settings.bottom_of_band = FREQ_87500_kHz;
			break;
		case BAND_76000_108000_kHz:
			si47xx_set_property(FM_SEEK_BAND_BOTTOM, 7600);
			si47xx_set_property(FM_SEEK_BAND_TOP, 10800);
			Si47xx_dev->settings.band = BAND_76000_108000_kHz;
			Si47xx_dev->settings.bottom_of_band = FREQ_76000_kHz;
			break;
		case BAND_76000_90000_kHz:
			si47xx_set_property(FM_SEEK_BAND_BOTTOM, 7600);
			si47xx_set_property(FM_SEEK_BAND_TOP, 9000);
			Si47xx_dev->settings.band = BAND_76000_90000_kHz;
			Si47xx_dev->settings.bottom_of_band = FREQ_76000_kHz;
			break;
		default:
			ret = -1;
		}
	}

	return ret;
}

int Si47xx_dev_ch_spacing_set(int ch_spacing)
{
	int ret = 0;
	u16 prev_ch_spacing = 0;

	debug("Si47xx_dev_ch_spacing_set called");

	mutex_lock(&(Si47xx_dev->lock));
	prev_ch_spacing = Si47xx_dev->settings.channel_spacing;
	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_ch_spacing_set called "
				"when DS is invalid");
		ret = -1;
	} else {
		switch (ch_spacing) {
		case CHAN_SPACING_200_kHz:
			si47xx_set_property(FM_SEEK_FREQ_SPACING, 20);
			Si47xx_dev->settings.channel_spacing =
							CHAN_SPACING_200_kHz;
			break;

		case CHAN_SPACING_100_kHz:
			si47xx_set_property(FM_SEEK_FREQ_SPACING, 10);
			Si47xx_dev->settings.channel_spacing =
							CHAN_SPACING_100_kHz;
			break;

		case CHAN_SPACING_50_kHz:
			si47xx_set_property(FM_SEEK_FREQ_SPACING, 5);
			Si47xx_dev->settings.channel_spacing =
							CHAN_SPACING_50_kHz;
			break;

		default:
				ret = -1;
		}

		if (ret == 0) {
			if (ret < 0) {
				dev_err(Si47xx_dev->dev, "Si47xx_dev_ch_spacing_set "
						"i2c_write 1 failed");
				Si47xx_dev->settings.channel_spacing =
					prev_ch_spacing;
			}
		}
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_chan_select(u32 frequency)
{
	int ret = 0;

	debug("Si47xx_dev_chan_select called");

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_chan_select called when DS is invalid");
		ret = -1;
	} else {
		Si47xx_dev->state.seek_state = RADIO_SEEK_ON;

		ret = tune_freq(frequency);
		debug("Si47xx_dev_chan_select called1");
		Si47xx_dev->state.seek_state = RADIO_SEEK_OFF;
	}
	return ret;
}

int Si47xx_dev_chan_get(u32 *frequency)
{
	int ret = 0;
	struct tune_data_t tune_data;

	debug("Si47xx_dev_chan_get called\n");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_chan_get called when DS is invalid");
		ret = -1;
	} else {
		if (ret < 0)
			debug("Si47xx_dev_chan_get i2c_read failed");
		else {
			ret = fmTuneStatus(0, 1, &tune_data);
			*frequency = tune_data.freq;
		}
	}
	mutex_unlock(&(Si47xx_dev->lock));
	return ret;
}

int Si47xx_dev_seek_full(u32 *frequency)
{
	int ret = 0;

	debug("Si47xx_dev_seek_full called\n");
	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_seek_full called when DS is invalid");
		ret = -1;
	} else {
		Si47xx_dev->state.seek_state = RADIO_SEEK_ON;
		ret = seek(frequency, 1, 0);
		Si47xx_dev->state.seek_state = RADIO_SEEK_OFF;
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_seek_up(u32 *frequency)
{
	int ret = 0;

	debug("Si47xx_dev_seek_up called\n");
	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_seek_up called when DS is invalid");
		ret = -1;
	} else {
		Si47xx_dev->state.seek_state = RADIO_SEEK_ON;
		ret = seek(frequency, 1, 1);
		Si47xx_dev->state.seek_state = RADIO_SEEK_OFF;
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_seek_down(u32 *frequency)
{
	int ret = 0;

	debug("Si47xx_dev_seek_down called\n");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_seek_down called when DS is invalid");
		ret = -1;
	} else {
		Si47xx_dev->state.seek_state = RADIO_SEEK_ON;

		ret = seek(frequency, 0, 1);

		Si47xx_dev->state.seek_state = RADIO_SEEK_OFF;
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_RSSI_seek_th_set(u8 seek_th)
{
	int ret = 0;

	debug("Si47xx_dev_RSSI_seek_th_set called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_RSSI_seek_th_set called "
				"when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_SEEK_TUNE_RSSI_THRESHOLD, seek_th);
		Si47xx_dev->settings.curr_rssi_th = seek_th;
		if (ret < 0)
			debug("Si47xx_dev_RSSI_seek_th_set i2c_write 1 failed");
	}
	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_seek_SNR_th_set(u8 seek_SNR)
{
	int ret = 0;

	debug("Si47xx_dev_seek_SNR_th_set called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_seek_SNR_th_set called "
				"when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_SEEK_TUNE_SNR_THRESHOLD, seek_SNR);
		Si47xx_dev->settings.curr_snr = seek_SNR;

		if (ret < 0)
			debug("Si47xx_dev_seek_SNR_th_set i2c_write 1 failed");
	}
	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

/*ToDo Don't use anymore*/
int Si47xx_dev_seek_FM_ID_th_set(u8 seek_FM_ID_th)
{
	int ret = 0;

	debug("Si47xx_dev_seek_FM_ID_th_set called");

	return ret;
}

int Si47xx_dev_cur_RSSI_get(struct rssi_snr_t *cur_RSSI)
{
	int ret = 0;

	struct rsq_data_t rsq_data;
	debug("Si47xx_dev_cur_RSSI_get called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_cur_RSSI_get called when DS is invalid");
		ret = -1;
	} else {
		fmRsqStatus(0, &rsq_data);
		cur_RSSI->curr_rssi = rsq_data.rssi;
		cur_RSSI->curr_rssi_th =
			Si47xx_dev->settings.curr_rssi_th;
		cur_RSSI->curr_snr = rsq_data.snr;

	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

/*ToDo Don't use anymore*/
int Si47xx_dev_device_id(struct device_id *dev_id)
{
	int ret = 0;

	debug("Si47xx_dev_device_id called");

	return ret;
}

/*ToDo Don't use anymore*/
int Si47xx_dev_chip_id(struct chip_id *chp_id)
{
	int ret = 0;

	debug("Si47xx_dev_chip_id called");

	return ret;
}

int Si47xx_dev_sys_config2(struct sys_config2 *sys_conf2)
{
	int ret = 0;

	debug("Si4709_sys_config2 called\n");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si4709_sys_config2 called when DS is invalid");
		ret = -1;
	} else {
		sys_conf2->rssi_th = Si47xx_dev->settings.curr_rssi_th;
		sys_conf2->fm_band = Si47xx_dev->settings.band;
		sys_conf2->fm_chan_spac =
			Si47xx_dev->settings.channel_spacing;
		sys_conf2->fm_vol = 0;
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_sys_config3(struct sys_config3 *sys_conf3)
{
	int ret = 0;

	debug("Si4709_sys_config3 called\n");
	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si4709_sys_config3 called when DS is invalid");
		ret = -1;
	} else {
		sys_conf3->smmute = 0;
		sys_conf3->smutea = 0;
		sys_conf3->volext = 0;
		sys_conf3->sksnr = Si47xx_dev->settings.curr_snr;
		sys_conf3->skcnt = 0;
	}
	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_status_rssi(struct status_rssi *status)
{
	int ret = 0;
	struct rsq_data_t rsq_data;

	debug("Si47xx_dev_status_rssi called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_status_rssi called when DS is invalid");
		mutex_unlock(&(Si47xx_dev->lock));
		return  -1;
	}
	fmRsqStatus(0, &rsq_data);

	pr_debug("%s: Si47xx_dev_status_rssi %d\n",
			__func__, rsq_data.rssi);

	Si47xx_dev->settings.curr_rssi = rsq_data.rssi;
	status->rssi = rsq_data.rssi;

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_sys_config2_set(struct sys_config2 *sys_conf2)
{
	int ret = 0;

	debug("Si47xx_dev_sys_config2_set called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_sys_config2_set called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_SEEK_TUNE_RSSI_THRESHOLD,
				sys_conf2->rssi_th);
		Si47xx_dev_band_set(sys_conf2->fm_band);
		si47xx_set_property(FM_SEEK_FREQ_SPACING,
			sys_conf2->fm_chan_spac);
		Si47xx_dev->settings.curr_rssi_th = sys_conf2->rssi_th;
		Si47xx_dev->settings.band = sys_conf2->fm_band;
		Si47xx_dev->settings.channel_spacing = sys_conf2->fm_chan_spac;
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_sys_config3_set(struct sys_config3 *sys_conf3)
{
	int ret = 0;

	debug("Si47xx_dev_sys_config3_set called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_sys_config3_set called "
				"when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_SEEK_TUNE_SNR_THRESHOLD,
			sys_conf3->sksnr);
		Si47xx_dev->settings.curr_snr = sys_conf3->sksnr;
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

/* VNVS:END */

/* VNVS:START 18-NOV'09 */
/* Reading AFCRL Bit */
int Si47xx_dev_AFCRL_get(u8 *afc)
{
	int ret = 0;
	struct rsq_data_t rsq_data;

	debug("Si47xx_dev_AFCRL_get called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_AFCRL_get called when DS is invalid");
		ret = -1;
	} else {
		fmRsqStatus(0, &rsq_data);
		*afc = rsq_data.afcrl;
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

/* Setting DE
   emphasis time constant 50us(Europe,Japan,Australia) or 75us(USA)
 */
int Si47xx_dev_DE_set(u8 de_tc)
{
	int ret = 0;

	debug("Si47xx_dev_DE_set called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_DE_set called when DS is invalid");
		ret = -1;
	} else {
		switch (de_tc) {
		case DE_TIME_CONSTANT_50:
			si47xx_set_property(FM_DEEMPHASIS, FM_DEEMPH_50US);
		break;

		case DE_TIME_CONSTANT_75:
			si47xx_set_property(FM_DEEMPHASIS, FM_DEEMPH_75US);
		break;

		default:
				ret = -1;
		}

		if (0 == ret) {
			if (ret < 0)
				dev_err(Si47xx_dev->dev, "%s failed %d\n",
				__func__, ret);
		}
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

/*Resetting the RDS Data Buffer*/
int Si47xx_dev_reset_rds_data()
{
	int ret = 0;

	debug_rds("Si47xx_dev_reset_rds_data called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_reset_rds_data called when DS is invalid");
		ret = -1;
	} else {
		RDS_Buffer_Index_write = 0;
		RDS_Buffer_Index_read = 0;
		RDS_Data_Lost = 0;
		RDS_Data_Available = 0;
		memset(RDS_Block_Data_buffer, 0, RDS_BUFFER_LENGTH * 8);
		memset(RDS_Block_Error_buffer, 0, RDS_BUFFER_LENGTH * 4);
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_volume_set(u8 volume)
{
	int ret = 0;

	debug("Si47xx_dev_volume_set called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_volume_set called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(RX_VOLUME, pSi47xxdata->rx_vol[volume] &
			RX_VOLUME_MASK);
		Si47xx_dev->vol_idx = volume;

		if (ret < 0)
			dev_err(Si47xx_dev->dev, "%s failed %d\n",
			__func__, ret);
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_volume_get(u8 *volume)
{
	int ret = 0;

	debug("Si4709_dev_volume_get called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si4709_dev_volume_get called when DS is invalid");
		ret = -1;
	} else
		*volume = Si47xx_dev->vol_idx;

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

/*
VNVS:START 19-AUG'10 : Adding DSMUTE ON/OFF feature.
The Soft Mute feature is available to attenuate the audio
outputs and minimize audible noise in very weak signal conditions.
 */
int Si47xx_dev_DSMUTE_ON(void)
{
	int ret = 0;

	debug("Si47xx_dev_DSMUTE_ON called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_DSMUTE_ON called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_SOFT_MUTE_RATE, 64);
		si47xx_set_property(FM_SOFT_MUTE_MAX_ATTENUATION, 0);
		si47xx_set_property(FM_SOFT_MUTE_SNR_THRESHOLD, 4);
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_DSMUTE_OFF(void)
{
	int ret = 0;

	debug("Si47xx_dev_DSMUTE_OFF called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_DSMUTE_OFF called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_SOFT_MUTE_RATE, 64);
		si47xx_set_property(FM_SOFT_MUTE_MAX_ATTENUATION, 16);
		si47xx_set_property(FM_SOFT_MUTE_SNR_THRESHOLD, 4);
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

/*VNVS:END*/

int Si47xx_dev_MUTE_ON(void)
{
	int ret = 0;

	debug("Si47xx_dev_MUTE_ON called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_MUTE_ON called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(RX_HARD_MUTE,
			RX_HARD_MUTE_RMUTE_MASK |
			RX_HARD_MUTE_LMUTE_MASK);
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_MUTE_OFF(void)
{
	int ret = 0;

	debug("Si47xx_dev_MUTE_OFF called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_MUTE_OFF called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(RX_HARD_MUTE, 0);
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_MONO_SET(void)
{
	int ret = 0;

	debug("Si47xx_dev_MONO_SET called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_MONO_SET called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_BLEND_MONO_THRESHOLD, 127);
		si47xx_set_property(FM_BLEND_STEREO_THRESHOLD, 127);
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_STEREO_SET(void)
{
	int ret = 0;

	debug("Si47xx_dev_STEREO_SET called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_STEREO_SET called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_BLEND_MONO_THRESHOLD, 30);
		si47xx_set_property(FM_BLEND_STEREO_THRESHOLD, 49);
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_RDS_ENABLE(void)
{
	int ret = 0;

	debug("Si47xx_dev_RDS_ENABLE called");

	mutex_lock(&(Si47xx_dev->lock));
	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_RDS_ENABLE called when DS is invalid");
		ret = -1;
	} else {
#ifdef RDS_INTERRUPT_ON_ALWAYS
		si47xx_set_property(GPO_IEN, GPO_IEN_STCIEN_MASK |
		GPO_IEN_STCREP_MASK | GPO_IEN_RDSIEN_MASK |
		GPO_IEN_RDSREP_MASK);
#endif
		si47xx_set_property(FM_RDS_INTERRUPT_SOURCE,
					FM_RDS_INTERRUPT_SOURCE_RECV_MASK);
		si47xx_set_property(FM_RDS_CONFIG, FM_RDS_CONFIG_RDSEN_MASK |
			(3 << FM_RDS_CONFIG_BLETHA_SHFT) |
			(3 << FM_RDS_CONFIG_BLETHB_SHFT) |
			(3 << FM_RDS_CONFIG_BLETHC_SHFT) |
			(3 << FM_RDS_CONFIG_BLETHD_SHFT));
#ifdef RDS_INTERRUPT_ON_ALWAYS
		Si47xx_RDS_flag = RDS_WAITING;
#endif
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_RDS_DISABLE(void)
{
	int ret = 0;

	debug("Si47xx_dev_RDS_DISABLE called");

	mutex_lock(&(Si47xx_dev->lock));
	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_RDS_DISABLE called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_RDS_CONFIG, 0);
#ifdef RDS_INTERRUPT_ON_ALWAYS
		Si47xx_RDS_flag = NO_WAIT;
#endif
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_rstate_get(struct dev_state_t *dev_state)
{
	int ret = 0;

	debug("Si47xx_dev_rstate_get called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_rstate_get called when DS is invalid");
		ret = -1;
	} else {
		dev_state->power_state = Si47xx_dev->state.power_state;
		dev_state->seek_state = Si47xx_dev->state.seek_state;
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

/* VNVS:START 7-JUNE'10 Function call for work-queue "Si47xx_wq" */
#ifdef RDS_INTERRUPT_ON_ALWAYS
void Si47xx_work_func(struct work_struct *work)
{
	struct radio_data_t rds_data;
	int i = 0;
	u8 RdsFifoUsed;
#ifdef RDS_TESTING
	u8 group_type;
#endif
	debug_rds("%s", __func__);
mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_RDS_data_get called when DS is invalid");
		return;
	}

	if (RDS_Data_Lost > 1)
		debug_rds("No_of_RDS_groups_Lost till now : %d",
				RDS_Data_Lost);
	fmRdsStatus(1, 0, &rds_data, &RdsFifoUsed);
	/* RDSR bit and RDS Block data, so reading the RDS registers */
	do {
		/* Writing into RDS_Block_Data_buffer */
		i = 0;
		RDS_Block_Data_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			rds_data.rdsa;
		RDS_Block_Data_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			rds_data.rdsb;
		RDS_Block_Data_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			rds_data.rdsc;
		RDS_Block_Data_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			rds_data.rdsd;

		/*Writing into RDS_Block_Error_buffer */
		i = 0;

		RDS_Block_Error_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			rds_data.blera;
		RDS_Block_Error_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			rds_data.blerb;
		RDS_Block_Error_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			rds_data.blerc;
		RDS_Block_Error_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			rds_data.blerd;
		fmRdsStatus(1, 0, &rds_data, &RdsFifoUsed);
	} while (RdsFifoUsed != 0);

#ifdef RDS_TESTING
	if (RDS_Block_Error_buffer
			[0 + 4 * RDS_Buffer_Index_write] < 3) {
		debug_rds("PI Code is %d",
				RDS_Block_Data_buffer[0 + 4
				* RDS_Buffer_Index_write]);
	}
	if (RDS_Block_Error_buffer
			[1 + 4 * RDS_Buffer_Index_write] < 2) {
		group_type = RDS_Block_Data_buffer[1 + 4
			* RDS_Buffer_Index_write] >> 11;

		if (group_type & 0x01) {
			debug_rds("PI Code is %d",
					RDS_Block_Data_buffer[2 + 4
					* RDS_Buffer_Index_write]);
		}
		if (group_type == GROUP_TYPE_2A
				|| group_type == GROUP_TYPE_2B) {
			if (RDS_Block_Error_buffer
					[2 + 4 * RDS_Buffer_Index_write] < 3) {
				debug_rds("Update RT with RDSC");
			} else {
				debug_rds("RDS_Block_Error_buffer"
						" of Block C is greater than 3");
			}
		}
	}
#endif
	RDS_Buffer_Index_write++;

	if (RDS_Buffer_Index_write >= RDS_BUFFER_LENGTH)
		RDS_Buffer_Index_write = 0;

	debug_rds("RDS_Buffer_Index_write = %d", RDS_Buffer_Index_write);
	mutex_unlock(&(Si47xx_dev->lock));
}
#endif
/*VNVS:END*/

int Si47xx_dev_RDS_data_get(struct radio_data_t *data)
{
	int i, ret = 0;
	struct tune_data_t tune_data;
	struct rsq_data_t rsq_data;

	debug_rds("Si47xx_dev_RDS_data_get called");

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		dev_err(Si47xx_dev->dev, "Si47xx_dev_RDS_data_get called when DS is invalid");
		mutex_unlock(&(Si47xx_dev->lock));
		return  -1;
	}
#ifdef RDS_INTERRUPT_ON_ALWAYS
	debug_rds("RDS_Buffer_Index_read = %d", RDS_Buffer_Index_read);

	/*If No New RDS Data is available return error */
	if (RDS_Buffer_Index_read == RDS_Buffer_Index_write) {
		debug_rds("No_New_RDS_Data_is_available");
		ret = fmTuneStatus(0, 1, &tune_data);
		data->curr_channel = tune_data.freq;
		fmRsqStatus(0, &rsq_data);
		data->curr_rssi = rsq_data.rssi;
		debug_rds("curr_channel: %u, curr_rssi:%u",
				data->curr_channel,
				(u32) data->curr_rssi);
		mutex_unlock(&(Si47xx_dev->lock));
		return -1;
	}

	ret = fmTuneStatus(0, 1, &tune_data);
	data->curr_channel = tune_data.freq;
	fmRsqStatus(0, &rsq_data);
	data->curr_rssi = rsq_data.rssi;
	debug_rds("curr_channel: %u, curr_rssi:%u",
			data->curr_channel, (u32) data->curr_rssi);

	/* Reading from RDS_Block_Data_buffer */
	i = 0;
	data->rdsa = RDS_Block_Data_buffer[i++ + 4
		* RDS_Buffer_Index_read];
	data->rdsb = RDS_Block_Data_buffer[i++ + 4
		* RDS_Buffer_Index_read];
	data->rdsc = RDS_Block_Data_buffer[i++ + 4
		* RDS_Buffer_Index_read];
	data->rdsd = RDS_Block_Data_buffer[i++ + 4
		* RDS_Buffer_Index_read];

	/* Reading from RDS_Block_Error_buffer */
	i = 0;
	data->blera = RDS_Block_Error_buffer[i++ + 4
		* RDS_Buffer_Index_read];
	data->blerb = RDS_Block_Error_buffer[i++ + 4
		* RDS_Buffer_Index_read];
	data->blerc = RDS_Block_Error_buffer[i++ + 4
		* RDS_Buffer_Index_read];
	data->blerd = RDS_Block_Error_buffer[i++ + 4
		* RDS_Buffer_Index_read];

	/*Flushing the read data */
	memset(&RDS_Block_Data_buffer[0 + 4 * RDS_Buffer_Index_read],
			0, 8);
	memset(&RDS_Block_Error_buffer[0 + 4 * RDS_Buffer_Index_read],
			0, 4);

	RDS_Buffer_Index_read++;

	if (RDS_Buffer_Index_read >= RDS_BUFFER_LENGTH)
		RDS_Buffer_Index_read = 0;

	debug_rds("RDS_Buffer_Index_read = %d", RDS_Buffer_Index_read);
#endif

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}

int Si47xx_dev_RDS_timeout_set(u32 time_out)
{
	int ret = 0;
	u32 jiffy_count = 0;

	debug("Si47xx_dev_RDS_timeout_set called");
	/****convert time_out(in milliseconds) into jiffies*****/

	jiffy_count = msecs_to_jiffies(time_out);

	debug("jiffy_count%d", jiffy_count);

	mutex_lock(&(Si47xx_dev->lock));

	if (Si47xx_dev->valid == eFALSE) {
		debug("Si47xx_dev_RDS_timeout_set called when DS is invalid");
		ret = -1;
	} else {
		Si47xx_dev->settings.timeout_RDS = jiffy_count;
	}

	mutex_unlock(&(Si47xx_dev->lock));

	return ret;
}