aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/samsung/fm_si4709/Si4705_dev.c
blob: 0898882dfc7c0de230ef00df129743cebe3a4c91 (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
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
#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 "Si4709_regs.h"
#include "Si4709_main.h"
#include "Si4709_dev.h"
#include "Si4709_common.h"

#if 1 /* To do */
#include "commanddefs.h"
#include "propertydefs.h"
#endif


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 RSSI_seek_th_MAX	0x7F
#define RSSI_seek_th_MIN	0x00

#define seek_SNR_th_DISB	0x00
#define seek_SNR_th_MIN		0x01	/*most stops */
#define seek_SNR_th_MAX		0x0F	/*fewest stops */

#define seek_FM_ID_th_DISB	0x00
#define seek_FM_ID_th_MAX	0x01	/*most stops */
#define seek_FM_ID_th_MIN	0x0F	/*fewest stops */

#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 Si4709_RDS_flag;
int RDS_Data_Available;
int RDS_Data_Lost;
int RDS_Groups_Available_till_now;
struct workqueue_struct *Si4709_wq;
struct work_struct Si4709_work;
#endif

u8 cmd[8];
u8 rsp[13];
u8 RsqInts;
u8 Status;
u8 SMUTE;
u8 AFCRL;
u8 Valid;
u8 Pilot;
u8 Blend;
u16 Freq;
u8 RSSI;
u8 ASNR;
u8 FreqOff;
u8 STC;
u8 BLTF;
u16 AntCap;
u8  RdsInts;
u8  RdsSync;
u8  GrpLost;
u8  RdsFifoUsed;
u16 BlockA;
u16 BlockB;
u16 BlockC;
u16 BlockD;
u8  BleA;
u8  BleB;
u8  BleC;
u8  BleD;



/*static functions*/
/**********************************************/
static void wait(void);

#ifndef RDS_INTERRUPT_ON_ALWAYS
static void wait_RDS(void);
#endif

static int powerup(void);
static int powerdown(void);

static int seek(u32 *, int, int);
static int tune_freq(u32);
static u16 freq_to_channel(u32);
static void fmTuneStatus(u8 cancel, u8 intack);
static void fmRsqStatus(u8 intack);
static void si47xx_set_property(u16 propNumber, u16 propValue);
static int si47xx_command(u8 cmd_size, u8 *cmd, u8 reply_size, u8 *reply);
static void fmRdsStatus(u8 intack, u8 mtfifo);
static void fmTuneFreq(u16 frequency);
static void fmSeekStart(u8 seekUp, u8 wrap);
static u8 si47xx_readStatus(void);
static void si47xx_waitForCTS(void);
static u8 getIntStatus(void);
static int i2c_write(u8 number_bytes, u8 *data_out);
static int i2c_read(u8 number_bytes, u8 *data_in);
/**********************************************/

/*Si4709 device structure*/
static struct Si4709_device_t Si4709_dev = {
	.client = NULL,
	.valid = eFALSE,
	.valid_client_state = eFALSE,
};

/*Wait flag*/
/*WAITING or WAIT_OVER or NO_WAIT*/
int Si4709_dev_wait_flag = NO_WAIT;
#ifdef RDS_INTERRUPT_ON_ALWAYS
int Si4709_RDS_flag = NO_WAIT;
#endif

unsigned int Si4709_dev_int;
unsigned int Si4709_dev_irq;

#if defined(CONFIG_MACH_M0)
unsigned int Si4709_dev_sw;
#endif

#if defined(CONFIG_MACH_M0_CTC)
static const u16 rx_vol[] = {
0x0, 0x15, 0x18, 0x1B, 0x1E, 0x21, 0x24, 0x27,
0x2A, 0x2D, 0x30, 0x33, 0x36, 0x39, 0x3C, 0x3F};
#else
static const u16 rx_vol[] = {
0x0, 0x13, 0x16, 0x19, 0x1C, 0x1F, 0x22, 0x25,
0x28, 0x2B, 0x2E, 0x31, 0x34, 0x37, 0x3A, 0x3D};
#endif


int Si4709_dev_init(struct i2c_client *client)
{
	int ret = 0;

	debug("Si4709_dev_init called");

	mutex_lock(&(Si4709_dev.lock));

	Si4709_dev.client = client;

#if defined(CONFIG_MACH_M0)
	if (system_rev >= 15)
		Si4709_dev_int = GPIO_FM_INT_REV15;
	else
#endif
		Si4709_dev_int = GPIO_FM_INT;

	Si4709_dev_irq = gpio_to_irq(Si4709_dev_int);

#if defined(CONFIG_MACH_M0) || defined(CONFIG_MACH_M0_CTC)
	Si4709_dev_sw = GPIO_FM_MIC_SW;
#endif

	disable_irq(Si4709_dev_irq);

	Si4709_dev.state.power_state = RADIO_POWERDOWN;
	Si4709_dev.state.seek_state = RADIO_SEEK_OFF;
	Si4709_dev.valid_client_state = eTRUE;

#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) {
		error("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) {
		error("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 */
	Si4709_wq = create_singlethread_workqueue("Si4709_wq");
	if (!Si4709_wq) {
		error("Not sufficient memory for Si4709_wq, work-queue");
		ret = -ENOMEM;
		kfree(RDS_Block_Error_buffer);
		kfree(RDS_Block_Data_buffer);
		goto EXIT;
	}

	/*Initialising work_queue */
	INIT_WORK(&Si4709_work, Si4709_work_func);

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

	mutex_unlock(&(Si4709_dev.lock));

	debug("Si4709_dev_init call over");

	return ret;
}

int Si4709_dev_exit(void)
{
	int ret = 0;

	debug("Si4709_dev_exit called");

	mutex_lock(&(Si4709_dev.lock));

	/* Temporary blocked by abnormal function call(E-CIM 2657654) */
	/* DW Shim. 2010.03.04 */
	/*    Si4709_dev.client = NULL;	*/

	/*    Si4709_dev.valid_client_state = eFALSE;	*/
	/*    Si4709_dev.valid = eFALSE;	*/

#ifdef RDS_INTERRUPT_ON_ALWAYS
	if (Si4709_wq)
		destroy_workqueue(Si4709_wq);

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

	mutex_unlock(&(Si4709_dev.lock));

	debug("Si4709_dev_exit call over");

	return ret;
}

void Si4709_dev_mutex_init(void)
{
	mutex_init(&(Si4709_dev.lock));
}

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

	debug("Si4709_dev_powerup called");

	if (!(RADIO_ON == Si4709_dev.state.power_state)) {
		ret = powerup();
		if (ret < 0) {
			debug("powerup failed");
		} else if (Si4709_dev.valid_client_state == eFALSE) {
			debug("Si4709_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( */
			/*      &Si4709_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);
			Si4709_dev.settings.band = BAND_87500_108000_kHz;
			Si4709_dev.settings.bottom_of_band = FREQ_87500_kHz;
			si47xx_set_property(FM_SEEK_FREQ_SPACING,
				CHAN_SPACING_100_kHz);
			Si4709_dev.settings.channel_spacing =
				CHAN_SPACING_100_kHz;

			/* SYSCONFIG3_BITSET_SKSNR( */
			/*      &Si4709_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);
			Si4709_dev.settings.timeout_RDS =
				msecs_to_jiffies(value);
			Si4709_dev.settings.curr_snr = TUNE_SNR_THRESHOLD;
			Si4709_dev.settings.curr_rssi_th = TUNE_RSSI_THRESHOLD;
			Si4709_dev_STEREO_SET();

/*this will write all the above registers */
			if (ret < 0)
				debug("Si4709_dev_powerup i2c_write 1 failed");
			else {
				Si4709_dev.valid = eTRUE;
#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");
	enable_irq(Si4709_dev_irq);
	si47xx_set_property(0xff00, 0);

	return ret;
}

int Si4709_dev_powerdown(void)
{
	int ret = 0;

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

	mutex_lock(&(Si4709_dev.lock));

	disable_irq(Si4709_dev_irq);

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_powerdown called when DS is invalid");
		ret = -1;
	} else {
		ret = powerdown();
		if (ret < 0)
			debug("powerdown failed");
	}
	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

int Si4709_dev_suspend(void)
{
	int ret = 0;

	debug("Si4709_dev_suspend called");

#ifndef _ENABLE_RDS_
	disable_irq(Si4709_dev_irq);
#endif

	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid_client_state == eFALSE) {
		debug("Si4709_dev_suspend called "
				"when DS(state, client) is invalid");
		ret = -1;
	}
	mutex_unlock(&(Si4709_dev.lock));

	debug("Si4709_dev_enable call over");

	return ret;
}

int Si4709_dev_resume(void)
{
	int ret = 0;

	debug("Si4709_dev_resume called");

#ifndef _ENABLE_RDS_
	enable_irq(Si4709_dev_irq);
#endif

	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid_client_state == eFALSE) {
		debug("Si4709_dev_resume called "
				"when DS(state, client) is invalid");
		ret = -1;
	}

	mutex_unlock(&(Si4709_dev.lock));
	debug("Si4709_dev_disable call over");

	return ret;
}

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

	debug("Si4709_dev_band_set called");

	prev_band = Si4709_dev.settings.band;
	prev_bottom_of_band = Si4709_dev.settings.bottom_of_band;
	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_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);
			Si4709_dev.settings.band = BAND_87500_108000_kHz;
			Si4709_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);
			Si4709_dev.settings.band = BAND_76000_108000_kHz;
			Si4709_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);
			Si4709_dev.settings.band = BAND_76000_90000_kHz;
			Si4709_dev.settings.bottom_of_band = FREQ_76000_kHz;
			break;
		default:
			ret = -1;
		}
	}

	return ret;
}

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

	debug("Si4709_dev_ch_spacing_set called");

	mutex_lock(&(Si4709_dev.lock));
	prev_ch_spacing = Si4709_dev.settings.channel_spacing;
	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_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);
			Si4709_dev.settings.channel_spacing =
							CHAN_SPACING_200_kHz;
			break;

		case CHAN_SPACING_100_kHz:
			si47xx_set_property(FM_SEEK_FREQ_SPACING, 10);
			Si4709_dev.settings.channel_spacing =
							CHAN_SPACING_100_kHz;
			break;

		case CHAN_SPACING_50_kHz:
			si47xx_set_property(FM_SEEK_FREQ_SPACING, 5);
			Si4709_dev.settings.channel_spacing =
							CHAN_SPACING_50_kHz;
			break;

		default:
				ret = -1;
		}

		if (ret == 0) {
			if (ret < 0) {
				debug("Si4709_dev_ch_spacing_set "
						"i2c_write 1 failed");
				Si4709_dev.settings.channel_spacing =
					prev_ch_spacing;
			}
		}
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

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

	debug("Si4709_dev_chan_select called");

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

		ret = tune_freq(frequency);
		debug("Si4709_dev_chan_select called1");
		Si4709_dev.state.seek_state = RADIO_SEEK_OFF;
	}
	return ret;
}

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

	debug("Si4709_dev_chan_get called");

	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_chan_get called when DS is invalid");
		ret = -1;
	} else {
		if (ret < 0)
			debug("Si4709_dev_chan_get i2c_read failed");
		else {
			fmTuneStatus(0, 0);
			*frequency = Freq;
		}
	}
	mutex_unlock(&(Si4709_dev.lock));
	return ret;
}

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

	debug("Si4709_dev_seek_full called\n");
	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_seek_full called when DS is invalid");
		ret = -1;
	} else {
		Si4709_dev.state.seek_state = RADIO_SEEK_ON;
		ret = seek(frequency, 1, 0);
		Si4709_dev.state.seek_state = RADIO_SEEK_OFF;
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

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

	debug("Si4709_dev_seek_up called\n");
	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_seek_up called when DS is invalid");
		ret = -1;
	} else {
		Si4709_dev.state.seek_state = RADIO_SEEK_ON;
		ret = seek(frequency, 1, 1);
		Si4709_dev.state.seek_state = RADIO_SEEK_OFF;
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

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

	debug("Si4709_dev_seek_down called\n");

	mutex_lock(&(Si4709_dev.lock));

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

		ret = seek(frequency, 0, 1);

		Si4709_dev.state.seek_state = RADIO_SEEK_OFF;
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

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

	debug("Si4709_dev_RSSI_seek_th_set called");

	mutex_lock(&(Si4709_dev.lock));

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

	return ret;
}

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

	debug("Si4709_dev_seek_SNR_th_set called");

	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_seek_SNR_th_set called "
				"when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_SEEK_TUNE_SNR_THRESHOLD, seek_SNR);
		Si4709_dev.settings.curr_snr = seek_SNR;

		if (ret < 0)
			debug("Si4709_dev_seek_SNR_th_set i2c_write 1 failed");
	}
	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

int Si4709_dev_seek_FM_ID_th_set(u8 seek_FM_ID_th)
{
	int ret = 0;

	debug("Si4709_dev_seek_FM_ID_th_set called");

	return ret;
}

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

	debug("Si4709_dev_cur_RSSI_get called");

	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_cur_RSSI_get called when DS is invalid");
		ret = -1;
	} else {
		fmRsqStatus(0);
		if (ret < 0) {
			debug("Si4709_dev_cur_RSSI_get i2c_read 1 failed");
		} else {
			cur_RSSI->curr_rssi = RSSI;
			cur_RSSI->curr_rssi_th =
				Si4709_dev.settings.curr_rssi_th;
			cur_RSSI->curr_snr = Si4709_dev.settings.curr_snr;
		}
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

/* VNVS:START 13-OCT'09 :
   Functions which reads device-id,chip-id,power configuration,
   system configuration2 registers
 */
int Si4709_dev_device_id(struct device_id *dev_id)
{
	int ret = 0;

	debug("Si4709_dev_device_id called");
#if 0 /*To Do*/
	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_device_id called when DS is invalid");
		ret = -1;
	} else {
		ret = i2c_read(DEVICE_ID);
		if (ret < 0) {
			debug("Si4709_dev_device_id i2c_read failed");
		} else {
			dev_id->part_number =
				DEVICE_ID_PART_NUMBER(Si4709_dev.
						registers[DEVICE_ID]);
			dev_id->manufact_number =
				DEVICE_ID_MANUFACT_NUMBER(Si4709_dev.
						registers[DEVICE_ID]);
		}
	}

	mutex_unlock(&(Si4709_dev.lock));
#endif
	return ret;
}

int Si4709_dev_chip_id(struct chip_id *chp_id)
{
	int ret = 0;

	debug("Si4709_dev_chip_id called");
#if 0 /*To Do*/
	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_chip_id called when DS is invalid");
		ret = -1;
	} else {
		ret = i2c_read(CHIP_ID);
		if (ret < 0) {
			debug("Si4709_dev_chip_id i2c_read failed");
		} else {
			chp_id->chip_version =
				CHIP_ID_CHIP_VERSION(
						Si4709_dev.registers[CHIP_ID]);
			chp_id->device =
				CHIP_ID_DEVICE(Si4709_dev.registers[CHIP_ID]);
			chp_id->firmware_version =
				CHIP_ID_FIRMWARE_VERSION(Si4709_dev.
						registers[CHIP_ID]);
		}
	}

	mutex_unlock(&(Si4709_dev.lock));
#endif
	return ret;
}

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

	debug("Si4709_sys_config2 called\n");

	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_sys_config2 called when DS is invalid");
		ret = -1;
	} else {
		if (ret < 0) {
			debug("Si4709_sys_config2 i2c_read failed");
		} else {
			sys_conf2->rssi_th = Si4709_dev.settings.curr_rssi_th;
			sys_conf2->fm_band = Si4709_dev.settings.band;
			sys_conf2->fm_chan_spac =
				Si4709_dev.settings.channel_spacing;
			sys_conf2->fm_vol = 0;
		}
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

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

	debug("Si4709_sys_config3 called\n");
	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_sys_config3 called when DS is invalid");
		mutex_unlock(&(Si4709_dev.lock));
		return  -1;
	}
	if (ret < 0) {
		debug("Si4709_sys_config3 i2c_read failed");
	} else {
		sys_conf3->smmute = 0;
		sys_conf3->smutea = 0;
		sys_conf3->volext = 0;
		sys_conf3->sksnr = Si4709_dev.settings.curr_snr;
		sys_conf3->skcnt = 0;
	}
	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

int Si4709_dev_status_rssi(struct status_rssi *status)
{
	int ret = 0;

	debug("Si4709_dev_status_rssi called");

	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_status_rssi called when DS is invalid");
		mutex_unlock(&(Si4709_dev.lock));
		return  -1;
	}
	fmRsqStatus(0);
	if (ret < 0) {
		debug("Si4709_sys_config3 i2c_read failed");
	} else {
		debug("Si4709_dev_status_rssi %d\n", RSSI);
		status->rssi = RSSI;
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

int Si4709_dev_sys_config2_set(struct sys_config2 *sys_conf2)
{
	int ret = 0;
	u16 register_bak = 0;

	debug("Si4709_dev_sys_config2_set called");

	mutex_lock(&(Si4709_dev.lock));

	register_bak = Si4709_dev.registers[SYSCONFIG2];

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_sys_config2_set called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_SEEK_TUNE_RSSI_THRESHOLD,
				sys_conf2->rssi_th);
		Si4709_dev_band_set(sys_conf2->fm_band);
		si47xx_set_property(FM_SEEK_FREQ_SPACING,
			sys_conf2->fm_chan_spac);
		Si4709_dev.settings.curr_rssi_th = sys_conf2->rssi_th;
		Si4709_dev.settings.band = sys_conf2->fm_band;
		Si4709_dev.settings.channel_spacing = sys_conf2->fm_chan_spac;

		if (ret < 0) {
			debug("Si4709_dev_sys_config2_set i2c_write 1 failed");
		} else
			debug(KERN_ERR "Si4709_dev_sys_config2_set() "
					": Write Sucess!!");
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

int Si4709_dev_sys_config3_set(struct sys_config3 *sys_conf3)
{
	int ret = 0;
	u16 register_bak = 0;

	debug("Si4709_dev_sys_config3_set called");

	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_sys_config3_set called "
				"when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_SEEK_TUNE_SNR_THRESHOLD,
			sys_conf3->sksnr);
		Si4709_dev.settings.curr_snr = sys_conf3->sksnr;
		if (ret < 0) {
			debug("Si4709_dev_sys_config3_set i2c_write 1 failed");
			Si4709_dev.registers[SYSCONFIG3] = register_bak;
		}
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

int Si4709_dev_power_config(struct power_config *pow_conf)
{
	int ret = 0;

	debug("Si4709_dev_power_config called");

	return ret;
}

/* VNVS:END */

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

	debug("Si4709_dev_AFCRL_get called");

	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_AFCRL_get called when DS is invalid");
		ret = -1;
	} else {
		fmRsqStatus(0);
		if (ret < 0)
			debug("Si4709_dev_AFCRL_get i2c_read failed");
		*afc = AFCRL;
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

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

	debug("Si4709_dev_DE_set called");

	mutex_lock(&(Si4709_dev.lock));

	sysconfig1 = Si4709_dev.registers[SYSCONFIG1];

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_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)
				debug("Si4709_dev_DE_set i2c_write failed");
		}
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

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

	debug_rds("Si4709_dev_reset_rds_data called");

	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_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(&(Si4709_dev.lock));

	return ret;
}

/*VNVS:END*/

int Si4709_dev_VOLEXT_ENB(void)
{
	int ret = 0;

	debug("Si4709_dev_VOLEXT_ENB called");

	return ret;
}

int Si4709_dev_VOLEXT_DISB(void)
{
	int ret = 0;

	debug("Si4709_dev_VOLEXT_DISB called");

	return ret;
}

int Si4709_dev_volume_set(u8 volume)
{
	int ret = 0;
	u16 sysconfig2 = 0;

	debug("Si4709_dev_volume_set called");

	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_volume_set called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(RX_VOLUME, rx_vol[volume] & RX_VOLUME_MASK);

		if (ret < 0) {
			debug("Si4709_dev_volume_set i2c_write failed");
			Si4709_dev.registers[SYSCONFIG2] = sysconfig2;
		}
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

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

	debug("Si4709_dev_volume_get called");
#if 0 /*To Do*/
	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_volume_get called when DS is invalid");
		ret = -1;
	} else
		*volume =
			SYSCONFIG2_GET_VOLUME(Si4709_dev.registers[SYSCONFIG2]);

	mutex_unlock(&(Si4709_dev.lock));
#endif
	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 Si4709_dev_DSMUTE_ON(void)
{
	int ret = 0;
	u16 powercfg = 0;

	debug("Si4709_dev_DSMUTE_ON called");

	mutex_lock(&(Si4709_dev.lock));

	powercfg = Si4709_dev.registers[POWERCFG];

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_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);
		if (ret < 0)
			error("Si4709_dev_DSMUTE_ON i2c_write failed");
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

int Si4709_dev_DSMUTE_OFF(void)
{
	int ret = 0;
	u16 powercfg = 0;

	debug("Si4709_dev_DSMUTE_OFF called");

	mutex_lock(&(Si4709_dev.lock));

	powercfg = Si4709_dev.registers[POWERCFG];

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_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);
		if (ret < 0)
			error("Si4709_dev_DSMUTE_OFF i2c_write failed");
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

/*VNVS:END*/

int Si4709_dev_MUTE_ON(void)
{
	int ret = 0;
	u16 powercfg = 0;

	debug("Si4709_dev_MUTE_ON called");

	mutex_lock(&(Si4709_dev.lock));

	powercfg = Si4709_dev.registers[POWERCFG];

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_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);
		if (ret < 0)
			debug("Si4709_dev_MUTE_ON i2c_write failed");
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

int Si4709_dev_MUTE_OFF(void)
{
	int ret = 0;
	u16 powercfg = 0;

	debug("Si4709_dev_MUTE_OFF called");

	mutex_lock(&(Si4709_dev.lock));

	powercfg = Si4709_dev.registers[POWERCFG];

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_MUTE_OFF called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(RX_HARD_MUTE, 0);
		if (ret < 0)
			debug("Si4709_dev_MUTE_OFF i2c_write failed");
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

int Si4709_dev_MONO_SET(void)
{
	int ret = 0;
	u16 powercfg = 0;

	debug("Si4709_dev_MONO_SET called");

	mutex_lock(&(Si4709_dev.lock));

	powercfg = Si4709_dev.registers[POWERCFG];

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_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);
		if (ret < 0)
			debug("Si4709_dev_MONO_SET i2c_write failed");
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

int Si4709_dev_STEREO_SET(void)
{
	int ret = 0;
	u16 powercfg = 0;

	debug("Si4709_dev_STEREO_SET called");

	mutex_lock(&(Si4709_dev.lock));

	powercfg = Si4709_dev.registers[POWERCFG];

	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_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);
		if (ret < 0)
			debug("Si4709_dev_STEREO_SET i2c_write failed");
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

int Si4709_dev_RDS_ENABLE(void)
{
	int ret = 0;

	debug("Si4709_dev_RDS_ENABLE called");

	mutex_lock(&(Si4709_dev.lock));
	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_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));
		if (ret < 0)
			debug("Si4709_dev_RDS_ENABLE i2c_write failed");
#ifdef RDS_INTERRUPT_ON_ALWAYS
		else
			Si4709_RDS_flag = RDS_WAITING;
#endif
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

int Si4709_dev_RDS_DISABLE(void)
{
	int ret = 0;

	debug("Si4709_dev_RDS_DISABLE called");

	mutex_lock(&(Si4709_dev.lock));
	if (Si4709_dev.valid == eFALSE) {
		debug("Si4709_dev_RDS_DISABLE called when DS is invalid");
		ret = -1;
	} else {
		si47xx_set_property(FM_RDS_CONFIG, 0);

		if (ret < 0)
			debug("Si4709_dev_RDS_DISABLE i2c_write failed");
#ifdef RDS_INTERRUPT_ON_ALWAYS
		else
			Si4709_RDS_flag = NO_WAIT;
#endif
	}

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

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

	debug("Si4709_dev_rstate_get called");

	mutex_lock(&(Si4709_dev.lock));

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

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

/* VNVS:START 7-JUNE'10 Function call for work-queue "Si4709_wq" */
#ifdef RDS_INTERRUPT_ON_ALWAYS
void Si4709_work_func(struct work_struct *work)
{
	int i = 0;
#ifdef RDS_TESTING
	u8 group_type;
#endif
	debug_rds("%s", __func__);
mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		error("Si4709_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);
	/* 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] =
			BlockA;
		RDS_Block_Data_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			BlockB;
		RDS_Block_Data_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			BlockC;
		RDS_Block_Data_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			BlockD;

		/*Writing into RDS_Block_Error_buffer */
		i = 0;

		RDS_Block_Error_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			BleA;
		RDS_Block_Error_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			BleB;
		RDS_Block_Error_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			BleC;
		RDS_Block_Error_buffer[i++ + 4 * RDS_Buffer_Index_write] =
			BleD;
		fmRdsStatus(1, 0);
	} 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(&(Si4709_dev.lock));
}
#endif
/*VNVS:END*/

int Si4709_dev_RDS_data_get(struct radio_data_t *data)
{
	int i, ret = 0;

	debug_rds("Si4709_dev_RDS_data_get called");

	mutex_lock(&(Si4709_dev.lock));

	if (Si4709_dev.valid == eFALSE) {
		error("Si4709_dev_RDS_data_get called when DS is invalid");
		mutex_unlock(&(Si4709_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");
		if (ret < 0)
			error("Si4709_dev_RDS_data_get i2c_read 1 failed");
		else {
			fmTuneStatus(0, 0);
			data->curr_channel = Freq;
			data->curr_rssi = RSSI;
			debug_rds("curr_channel: %u, curr_rssi:%u",
					data->curr_channel,
					(u32) data->curr_rssi);
		}
		mutex_unlock(&(Si4709_dev.lock));
		return -1;
	}
	if (ret < 0)
		error("Si4709_dev_RDS_data_get i2c_read 2 failed");
	else {
		fmTuneStatus(0, 0);
		data->curr_channel = Freq;
		data->curr_rssi = 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);
#else
#if 0 /*To Do*/
	SYSCONFIG1_BITSET_RDSIEN_HIGH(&Si4709_dev.registers[SYSCONFIG1]);

	ret = i2c_write(SYSCONFIG1);
	if (ret < 0) {
		debug("Si4709_dev_RDS_data_get i2c_write 1 failed");
		Si4709_dev.registers[SYSCONFIG1] = sysconfig1;
	} else {
		ret = i2c_read(SYSCONFIG1);
		if (ret < 0)
			debug("Si4709_dev_RDS_data_get i2c_read 1 failed");

		debug("sysconfig1: 0x%x", Si4709_dev.registers[SYSCONFIG1]);

		sysconfig1 = Si4709_dev.registers[SYSCONFIG1];

		Si4709_dev_wait_flag = RDS_WAITING;

		wait_RDS();

		ret = i2c_read(STATUSRSSI);
		if (ret < 0)
			debug("Si4709_dev_RDS_data_get i2c_read 2 failed");

		debug("statusrssi: 0x%x", Si4709_dev.registers[STATUSRSSI]);

		SYSCONFIG1_BITSET_RDSIEN_LOW(&Si4709_dev.registers[SYSCONFIG1]);

		ret = i2c_write(SYSCONFIG1);
		if (ret < 0) {
			debug("Si4709_dev_RDS_data_get i2c_write 2 failed");
			Si4709_dev.registers[SYSCONFIG1] = sysconfig1;
		} else if (Si4709_dev_wait_flag == WAIT_OVER) {
			Si4709_dev_wait_flag = NO_WAIT;

			ret = i2c_read(RDSD);
			if (ret < 0)
				debug("Si4709_dev_RDS_data_get "
					"i2c_read 3 failed");
			else {
				data->rdsa = Si4709_dev.registers[RDSA];
				data->rdsb = Si4709_dev.registers[RDSB];
				data->rdsc = Si4709_dev.registers[RDSC];
				data->rdsd = Si4709_dev.registers[RDSD];

				get_cur_chan_freq(&(data->curr_channel),
						Si4709_dev.registers[READCHAN]);
				debug("curr_channel: %u", data->curr_channel);
				data->curr_rssi =
					STATUSRSSI_RSSI_SIGNAL_STRENGTH
					(Si4709_dev.registers[STATUSRSSI]);
				debug("curr_rssi:%u", (u32)data->curr_rssi);
				data->blera =
					STATUSRSSI_RDS_BLOCK_A_ERRORS
					(Si4709_dev.registers[STATUSRSSI]);
				data->blerb =
					READCHAN_BLOCK_B_ERRORS(Si4709_dev.
							registers[READCHAN]);
				data->blerc =
					READCHAN_BLOCK_C_ERRORS(Si4709_dev.
							registers[READCHAN]);
				data->blerd =
					READCHAN_BLOCK_D_ERRORS(Si4709_dev.
							registers[READCHAN]);
			}
		} else {
			debug("Si4709_dev_RDS_data_get failure "
				"no interrupt or timeout");
			Si4709_dev_wait_flag = NO_WAIT;
			mutex_unlock(&(Si4709_dev.lock));
			return -1;
		}
	}
#endif
#endif

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

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

	debug("Si4709_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(&(Si4709_dev.lock));

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

	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

/**************************************************************/
static int powerup(void)
{
	int ret = 0;
	u16 powercfg = Si4709_dev.registers[POWERCFG];

#if defined(CONFIG_MACH_M0) || defined(CONFIG_MACH_M0_CTC)
	gpio_set_value(Si4709_dev_sw, GPIO_LEVEL_HIGH);
#endif

	gpio_set_value(GPIO_FM_RST, GPIO_LEVEL_LOW);
	usleep_range(5, 10);
	s3c_gpio_cfgpin(Si4709_dev_int, S3C_GPIO_OUTPUT);
	s3c_gpio_setpull(Si4709_dev_int, S3C_GPIO_PULL_DOWN);
	gpio_set_value(Si4709_dev_int, GPIO_LEVEL_LOW);
	usleep_range(10, 15);
	gpio_set_value(GPIO_FM_RST, GPIO_LEVEL_HIGH);
	usleep_range(5, 10);
	s3c_gpio_cfgpin(Si4709_dev_int, S3C_GPIO_SFN(0xF));
	s3c_gpio_setpull(Si4709_dev_int, S3C_GPIO_PULL_UP);
	usleep_range(10, 15);

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

	if (ret < 0) {
		debug("powerup->i2c_write 1 failed");
		Si4709_dev.registers[POWERCFG] = powercfg;
	} else {
		/* Si4709/09 datasheet: Table 7 */
		msleep(110);
		Si4709_dev.state.power_state = RADIO_ON;
	}
	return ret;
}

static int powerdown(void)
{
	int ret = 0;

	if (!(RADIO_POWERDOWN == Si4709_dev.state.power_state)) {
		cmd[0] = POWER_DOWN;
		ret = si47xx_command(1, cmd, 1, rsp);
		msleep(110);

		if (ret < 0)
			debug("powerdown->i2c_write failed");
		else
			Si4709_dev.state.power_state = RADIO_POWERDOWN;

		gpio_set_value(GPIO_FM_RST, GPIO_LEVEL_LOW);
	} else
		debug("Device already Powered-OFF\n");

#if defined(CONFIG_MACH_M0) || defined(CONFIG_MACH_M0_CTC)
	gpio_set_value(Si4709_dev_sw, GPIO_LEVEL_LOW);
#endif

	return ret;
}

static int seek(u32 *frequency, int up, int mode)
{
	int ret = 0;
	u8 get_int;

	if (ret < 0) {
		debug("seek i2c_write 1 failed");
	} else {
		Si4709_dev_wait_flag = SEEK_WAITING;
		fmSeekStart(up, mode); /* mode 0 is full scan */
		wait();
		do {
			get_int = getIntStatus();
		} while (!(get_int & STCINT));

		if (Si4709_dev_wait_flag == SEEK_CANCEL) {
			fmTuneStatus(1, 0);
			if (ret < 0) {
				debug("seek i2c_write 2 failed");
			}
			if (ret < 0)
				debug("seek i2c_read 1 failed");
			else
				*frequency = Freq;

			*frequency = 0;
		}

		Si4709_dev_wait_flag = NO_WAIT;

		fmTuneStatus(0, 1);
		if (BLTF != 1)
			*frequency = Freq;

		else {
			if (Valid)
				*frequency = Freq;
			else
				*frequency = 0;
		}
	}
	return ret;
}

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

	u16 channel = Si4709_dev.registers[CHANNEL];
	mutex_lock(&(Si4709_dev.lock));

	channel = freq_to_channel(frequency);
	if (ret < 0) {
		debug("tune_freq i2c_write 1 failed");
		Si4709_dev.registers[CHANNEL] = channel;
	} else {
		Si4709_dev_wait_flag = TUNE_WAITING;
		fmTuneFreq(frequency);
		wait();
		Si4709_dev_wait_flag = NO_WAIT;
		debug("Si4709_dev_wait_flag = TUNE_WAITING\n");

		if (!(getIntStatus() & STCINT)) {
			printk(KERN_INFO "%s tune is failed!\n", __func__);
			fmTuneStatus(1, 1);
			mutex_unlock(&(Si4709_dev.lock));
			return -1;
		}

		fmTuneStatus(0, 1);

		debug("Si4709 tune_freq fmTuneStatus %x\n", rsp[0]);

	}
	mutex_unlock(&(Si4709_dev.lock));

	return ret;
}

static u16 freq_to_channel(u32 frequency)
{
	u16 channel;

	if (frequency < Si4709_dev.settings.bottom_of_band)
		frequency = Si4709_dev.settings.bottom_of_band;

	channel = (frequency - Si4709_dev.settings.bottom_of_band)
		/ Si4709_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(Si4709_waitq,
			(Si4709_dev_wait_flag == WAIT_OVER) ||
			(Si4709_dev_wait_flag == SEEK_CANCEL));
}

#ifndef RDS_INTERRUPT_ON_ALWAYS
static void wait_RDS(void)
{
	wait_event_interruptible_timeout(Si4709_waitq,
			(Si4709_dev_wait_flag == WAIT_OVER),
			Si4709_dev.settings.timeout_RDS);
}
#endif

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

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

	debug("Si4709 getIntStatus return %x\n", rsp[0]);
	return rsp[0];
}

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

 Inputs:
 frequency in 10kHz steps
-----------------------------------------------------------------------------*/
static void fmTuneFreq(u16 frequency)
{
	cmd[0] = FM_TUNE_FREQ;
	cmd[1] = 0;
	cmd[2] = (u8)(frequency >> 8);
	cmd[3] = (u8)(frequency & 0x00FF);
	cmd[4] = (u8)0;
	si47xx_command(5, cmd, 1, rsp);
}

/*-----------------------------------------------------------------------------
 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 void fmTuneStatus(u8 cancel, u8 intack)
{
	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;

	si47xx_command(2, cmd, 8, rsp);

	STC    = !!(rsp[0] & STCINT);
	BLTF   = !!(rsp[1] & FM_TUNE_STATUS_OUT_BTLF);
	AFCRL  = !!(rsp[1] & FM_TUNE_STATUS_OUT_AFCRL);
	Valid  = !!(rsp[1] & FM_TUNE_STATUS_OUT_VALID);
	Freq   = ((u16)rsp[2] << 8) | (u16)rsp[3];
	RSSI   = rsp[4];
	ASNR   = rsp[5];
	AntCap = 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.
-----------------------------------------------------------------------------*/
void fmRdsStatus(u8 intack, u8 mtfifo)
{
	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;

	si47xx_command(2, cmd, 13, rsp);

	Status      = rsp[0];
	RdsInts     = rsp[1];
	RdsSync     = !!(rsp[2] & FM_RDS_STATUS_OUT_SYNC);
	GrpLost     = !!(rsp[2] & FM_RDS_STATUS_OUT_GRPLOST);
	RdsFifoUsed = rsp[3];
	BlockA      = ((u16)rsp[4] << 8) | (u16)rsp[5];
	BlockB      = ((u16)rsp[6] << 8) | (u16)rsp[7];
	BlockC      = ((u16)rsp[8] << 8) | (u16)rsp[9];
	BlockD      = ((u16)rsp[10] << 8) | (u16)rsp[11];
	BleA        = (rsp[12] & FM_RDS_STATUS_OUT_BLEA) >>
		FM_RDS_STATUS_OUT_BLEA_SHFT;
	BleB        = (rsp[12] & FM_RDS_STATUS_OUT_BLEB) >>
		FM_RDS_STATUS_OUT_BLEB_SHFT;
	BleC        = (rsp[12] & FM_RDS_STATUS_OUT_BLEC) >>
		FM_RDS_STATUS_OUT_BLEC_SHFT;
	BleD        = (rsp[12] & FM_RDS_STATUS_OUT_BLED) >>
		FM_RDS_STATUS_OUT_BLED_SHFT;
}


/*-----------------------------------------------------------------------------
 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:
Status:  Contains bits about the status returned from the part.
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)
{
	cmd[0] = FM_RSQ_STATUS;
	cmd[1] = 0;
	if (intack)
		cmd[1] |= FM_RSQ_STATUS_IN_INTACK;

	si47xx_command(2, cmd, 8, rsp);

	Status  = rsp[0];
	RsqInts = rsp[1];
	SMUTE   = !!(rsp[2] & FM_RSQ_STATUS_OUT_SMUTE);
	AFCRL   = !!(rsp[2] & FM_RSQ_STATUS_OUT_AFCRL);
	Valid   = !!(rsp[2] & FM_RSQ_STATUS_OUT_VALID);
	Pilot   = !!(rsp[3] & FM_RSQ_STATUS_OUT_PILOT);
	Blend   = rsp[3] & FM_RSQ_STATUS_OUT_STBLEND;
	RSSI    = rsp[4];
	ASNR    = rsp[5];
	FreqOff = rsp[7];
}


/*-----------------------------------------------------------------------------
 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 void fmSeekStart(u8 seekUp, u8 wrap)
{
	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;

	si47xx_command(2, cmd, 1, rsp);
}


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

 Inputs:
      propNumber:  The number identifying the property to set
      propValue:   The value of the property.
-----------------------------------------------------------------------------*/
void si47xx_set_property(u16 propNumber, u16 propValue)
{
	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);

	si47xx_command(6, cmd, 0, NULL);
}

/*-----------------------------------------------------------------------------
 This command returns the status
-----------------------------------------------------------------------------*/
u8 si47xx_readStatus(void)
{
	u8 status;
	i2c_read(1, &status);

	return status;
}


/*-----------------------------------------------------------------------------
 Command that will wait for CTS before returning
-----------------------------------------------------------------------------*/
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
-----------------------------------------------------------------------------*/
int si47xx_command(u8 cmd_size, u8 *cmd, u8 reply_size, u8 *reply)
{
	int ret = 0;
	ret = i2c_write(cmd_size, cmd);
	si47xx_waitForCTS();

	if (reply_size)
		i2c_read(reply_size, reply);

	if (ret < 0)
		printk(KERN_INFO "%s i2c_write failed %d\n", __func__, ret);

	return ret;
}

static int i2c_write(u8 number_bytes, u8 *data_out)
{
	int ret = 0;

	ret = i2c_master_send((struct i2c_client *)(Si4709_dev.client),
			(const char *)data_out, number_bytes);
	if (ret == number_bytes)
		ret = 0;
	else
		ret = -1;

	return ret;
}

static int i2c_read(u8 number_bytes, u8 *data_in)
{
	int ret = 0;

	ret = i2c_master_recv((struct i2c_client *)(Si4709_dev.client), data_in,
			number_bytes);
	if (ret < 0)
		printk(KERN_INFO "%s i2c_read failed %d\n", __func__, ret);

	return ret;
}