summaryrefslogtreecommitdiff
path: root/drivers/char/mxc_si4702.c
blob: 7d36f2902bb480b8aab4b8b02726dab480f131cc (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
/*
 * linux/drivers/char/mxc_si4702.c
 *
 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/cdev.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/regulator/consumer.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/mxc_si4702.h>
#include <linux/fsl_devices.h>
#include <linux/uaccess.h>

#define SI4702_DEV_NAME		"si4702"
#define DEV_MAJOR		0	/* this could be module param */
#define DEV_MINOR		0
#define DEV_BASE_MINOR		0
#define DEV_MINOR_COUNT		256
#define SI4702_I2C_ADDR	0x10	/* 7bits I2C address */
#define DELAY_WAIT	0xffff	/* loop_counter max value */
/* register define */
#define SI4702_DEVICEID		0x00
#define SI4702_CHIPID			0x01
#define SI4702_POWERCFG		0x02
#define SI4702_CHANNEL		0x03
#define SI4702_SYSCONFIG1		0x04
#define SI4702_SYSCONFIG2		0x05
#define SI4702_SYSCONFIG3		0x06
#define SI4702_TEST1			0x07
#define SI4702_TEST2			0x08
#define SI4702_B00TCONFIG		0x09
#define SI4702_STATUSRSSI		0x0A
#define SI4702_READCHAN		0x0B
#define SI4702_REG_NUM		0x10
#define SI4702_REG_BYTE		(SI4702_REG_NUM * 2)
#define SI4702_DEVICE_ID		0x1242
#define SI4702_RW_REG_NUM	(SI4702_STATUSRSSI - SI4702_POWERCFG)
#define SI4702_RW_OFFSET	\
	(SI4702_REG_NUM - SI4702_STATUSRSSI + SI4702_POWERCFG)

#define SI4702_SPACE_MASK	0x0030
#define SI4702_SPACE_200K	0x0
#define SI4702_SPACE_100K	0x10
#define SI4702_SPACE_50K	0x20

#define SI4702_BAND_MASK	0x00c0
#define SI4702_BAND_LSB		6

#define SI4702_SEEKTH_MASK	0xff00
#define SI4702_SEEKTH_LSB	8

#define SI4702_SNR_MASK		0x00f0
#define SI4702_SNR_LSB		4

#define SI4702_CNT_MASK		0x000f
#define SI4702_CNT_LSB		0

#define SI4702_VOL_MASK		0x000f
#define SI4702_VOL_LSB		0

#define SI4702_CHAN_MASK	0x03ff
#define SI4702_TUNE_BIT		0x8000
#define SI4702_STC_BIT		0x4000
#define SI4702_DMUTE_BIT	0x4000
#define SI4702_SEEKUP_BIT	0x0200
#define SI4702_SEEK_BIT		0x0100
#define SI4702_SF_BIT		0x2000
#define SI4702_ENABLE_BIT	0x0001
#define SI4702_DISABLE_BIT	0x0040

enum {
	BAND_USA = 0,
	BAND_JAP_W,
	BAND_JAP
};

struct si4702_info {
	int min_band;
	int max_band;
	int space;
	int volume;
	int channel;
	int mute;
};

struct si4702_drvdata {
	struct regulator *vio;
	struct regulator *vdd;
	struct class *radio_class;
	struct si4702_info info;
	/*by default, dev major is zero, and it's alloc dynamicaly. */
	int major;
	int minor;
	struct cdev *cdev;
	int count;		/* open count */
	struct i2c_client *client;
	unsigned char reg_rw_buf[SI4702_REG_BYTE];
	struct mxc_fm_platform_data *plat_data;
};

static struct si4702_drvdata *si4702_drvdata;

DEFINE_SPINLOCK(count_lock);

#ifdef DEBUG
static void si4702_dump_reg(void)
{
	int i, j;
	unsigned char *reg_rw_buf;

	if (NULL == si4702_drvdata)
		return;

	reg_rw_buf = si4702_drvdata->reg_rw_buf;

	for (i = 0; i < 10; i++) {
		j = i * 2 + 12;
		pr_debug("reg[%02d] = %04x\n", i,
			 ((reg_rw_buf[j] << 8) & 0xFF00) +
			 (reg_rw_buf[j + 1] & 0x00FF));
	}
	for (; i < 16; i++) {
		j = (i - 10) * 2;
		pr_debug("reg[%02d] = %04x\n", i,
			 ((reg_rw_buf[j] << 8) & 0xFF00) +
			 (reg_rw_buf[j + 1] & 0x00FF));
	}
}
#else
static void si4702_dump_reg(void)
{
}
#endif /* DEBUG */

/*
 *check the si4702 spec for the read/write concequence.
 *
 *0 2       A    F0         A    F
 *-------------------------------
 *      buf:0 2      A     F
 */
#define REG_to_BUF(reg) (((reg >= 0) && (reg < SI4702_STATUSRSSI)) ? \
		(reg - SI4702_STATUSRSSI + SI4702_REG_NUM) : \
		((reg >= SI4702_STATUSRSSI) && (reg < SI4702_REG_NUM)) ? \
		(reg - SI4702_STATUSRSSI) : -1)

static int si4702_read_reg(const int reg, u16 *value)
{
	int ret, index;
	unsigned char *reg_rw_buf;

	if (NULL == si4702_drvdata)
		return -1;

	reg_rw_buf = si4702_drvdata->reg_rw_buf;

	index = REG_to_BUF(reg);

	if (-1 == index)
		return -1;

	ret =
	    i2c_master_recv(si4702_drvdata->client, reg_rw_buf,
			    SI4702_REG_BYTE);

	*value = (reg_rw_buf[index * 2] << 8) & 0xFF00;
	*value |= reg_rw_buf[index * 2 + 1] & 0x00FF;

	return ret < 0 ? ret : 0;
}

static int si4702_write_reg(const int reg, const u16 value)
{
	int index, ret;
	unsigned char *reg_rw_buf;

	if (NULL == si4702_drvdata)
		return -1;

	reg_rw_buf = si4702_drvdata->reg_rw_buf;

	index = REG_to_BUF(reg);

	if (-1 == index)
		return -1;

	reg_rw_buf[index * 2] = (value & 0xFF00) >> 8;
	reg_rw_buf[index * 2 + 1] = value & 0x00FF;

	ret = i2c_master_send(si4702_drvdata->client,
			      &reg_rw_buf[SI4702_RW_OFFSET * 2],
			      (SI4702_STATUSRSSI - SI4702_POWERCFG) * 2);
	return ret < 0 ? ret : 0;
}

static void si4702_gpio_get(void)
{
	if (NULL == si4702_drvdata)
		return;

	si4702_drvdata->plat_data->gpio_get();
}

static void si4702_gpio_put(void)
{
	if (NULL == si4702_drvdata)
		return;

	si4702_drvdata->plat_data->gpio_put();
}

static void si4702_reset(void)
{
	if (NULL == si4702_drvdata)
		return;

	si4702_drvdata->plat_data->reset();
}

static void si4702_clock_en(int flag)
{
	if (NULL == si4702_drvdata)
		return;

	si4702_drvdata->plat_data->clock_ctl(flag);
}

static int si4702_id_detect(struct i2c_client *client)
{
	int ret, index;
	unsigned int ID = 0;
	unsigned char reg_rw_buf[SI4702_REG_BYTE];

	si4702_gpio_get();
	si4702_reset();
	si4702_clock_en(1);

	ret = i2c_master_recv(client, (char *)reg_rw_buf, SI4702_REG_BYTE);

	si4702_gpio_put();

	if (ret < 0)
		return ret;

	index = REG_to_BUF(SI4702_DEVICEID);
	if (index < 0)
		return index;

	ID = (reg_rw_buf[index * 2] << 8) & 0xFF00;
	ID |= reg_rw_buf[index * 2 + 1] & 0x00FF;

	return ID;
}

/* valid args 50/100/200 */
static int si4702_set_space(int space)
{
	u16 reg;
	int ret;
	struct si4702_info *info;

	if (NULL == si4702_drvdata)
		return -1;

	ret = si4702_read_reg(SI4702_SYSCONFIG2, &reg);
	if (ret == -1)
		return ret;

	reg &= ~SI4702_SPACE_MASK;
	switch (space) {
	case 50:
		reg |= SI4702_SPACE_50K;
		break;
	case 100:
		reg |= SI4702_SPACE_100K;
		break;
	case 200:
		ret |= SI4702_SPACE_200K;
		break;
	default:
		return -1;
	}

	ret = si4702_write_reg(SI4702_SYSCONFIG2, reg);
	if (ret == -1)
		return ret;

	info = &si4702_drvdata->info;
	info->space = space;
	return 0;
}

static int si4702_set_band_range(int band)
{
	u16 reg;
	int ret, band_min, band_max;
	struct si4702_info *info;

	if (NULL == si4702_drvdata)
		return -1;

	switch (band) {
	case BAND_USA:
		band_min = 87500;
		band_max = 108000;
		break;
	case BAND_JAP_W:
		band_min = 76000;
		band_max = 108000;
		break;
	case BAND_JAP:
		band_min = 76000;
		band_max = 90000;
		break;
	default:
		return -1;
	}

	ret = si4702_read_reg(SI4702_SYSCONFIG2, &reg);
	if (ret == -1)
		return ret;

	reg = (reg & ~SI4702_BAND_MASK)
	    | ((band << SI4702_BAND_LSB) & SI4702_BAND_MASK);
	ret = si4702_write_reg(SI4702_SYSCONFIG2, reg);
	if (ret == -1)
		return ret;

	info = &si4702_drvdata->info;
	info->min_band = band_min;
	info->max_band = band_max;
	return 0;
}

static int si4702_set_seekth(u8 seekth)
{
	u16 reg;
	int ret;

	if (NULL == si4702_drvdata)
		return -1;

	ret = si4702_read_reg(SI4702_SYSCONFIG2, &reg);
	if (ret == -1)
		return ret;

	reg =
	    (reg & ~SI4702_SEEKTH_MASK) | ((seekth << SI4702_SEEKTH_LSB) &
					   SI4702_SEEKTH_MASK);
	ret = si4702_write_reg(SI4702_SYSCONFIG2, reg);
	if (ret == -1)
		return ret;

	return 0;
}

static int si4702_set_sksnr(u8 sksnr)
{
	u16 reg;
	int ret;

	if (NULL == si4702_drvdata)
		return -1;

	ret = si4702_read_reg(SI4702_SYSCONFIG3, &reg);
	if (ret == -1)
		return ret;

	reg =
	    (reg & ~SI4702_SNR_MASK) | ((sksnr << SI4702_SNR_LSB) &
					SI4702_SNR_MASK);
	ret = si4702_write_reg(SI4702_SYSCONFIG3, reg);
	if (ret == -1)
		return ret;

	return 0;
}

static int si4702_set_skcnt(u8 skcnt)
{
	u16 reg;
	int ret;

	if (NULL == si4702_drvdata)
		return -1;

	ret = si4702_read_reg(SI4702_SYSCONFIG3, &reg);
	if (ret == -1)
		return ret;

	reg = (reg & ~SI4702_CNT_MASK) | (skcnt & SI4702_CNT_MASK);
	ret = si4702_write_reg(SI4702_SYSCONFIG3, reg);
	if (ret == -1)
		return ret;

	return 0;
}

static int si4702_set_vol(int vol)
{
	u16 reg;
	int ret;
	struct si4702_info *info;

	if (NULL == si4702_drvdata)
		return -1;

	ret = si4702_read_reg(SI4702_SYSCONFIG2, &reg);
	if (ret == -1)
		return ret;

	reg = (reg & ~SI4702_VOL_MASK) | (vol & SI4702_VOL_MASK);
	ret = si4702_write_reg(SI4702_SYSCONFIG2, reg);
	if (ret == -1)
		return ret;

	info = &si4702_drvdata->info;
	info->volume = vol;

	return 0;
}

static u8 si4702_channel_select(u32 freq)
{
	u16 loop_counter = 0;
	s16 channel;
	u16 si4702_reg_data;
	u8 error_ind = 0;
	struct i2c_client *client;
	struct si4702_info *info;

	if (NULL == si4702_drvdata)
		return -1;

	info = &si4702_drvdata->info;
	client = si4702_drvdata->client;

	dev_info(&client->dev, "Input frequnce is %d\n", freq);
	if (freq < 76000 || freq > 108000) {
		dev_err(&client->dev, "Input frequnce is invalid\n");
		return -1;
	}
	/* convert freq to channel */
	channel = (freq - info->min_band) / info->space;

	si4702_reg_data = SI4702_TUNE_BIT | (channel & SI4702_CHAN_MASK);
	/* set channel */
	error_ind = si4702_write_reg(SI4702_CHANNEL, si4702_reg_data);
	if (error_ind) {
		dev_err(&client->dev, "Failed to set channel\n");
		return -1;
	}
	dev_info(&client->dev, "Set channel to %d\n", channel);

	/* wait for STC == 1 */
	do {
		error_ind =
		    si4702_read_reg(SI4702_STATUSRSSI, &si4702_reg_data);

		if (error_ind) {
			dev_err(&client->dev, "Failed to read setted STC\n");
			return -1;
		}
		if ((si4702_reg_data & SI4702_STC_BIT) != 0)
			break;
	} while (++loop_counter < DELAY_WAIT);

	/* check loop_counter */
	if (loop_counter >= DELAY_WAIT) {
		dev_err(&client->dev, "Can't wait for STC bit set");
		return -1;
	}
	dev_info(&client->dev, "loop counter %d\n", loop_counter);

	loop_counter = 0;
	/* clear tune bit */
	error_ind = si4702_write_reg(SI4702_CHANNEL, 0);

	if (error_ind) {
		dev_err(&client->dev, "Failed to set stop tune\n");
		return -1;
	}

	/* wait for STC == 0 */
	do {
		error_ind =
		    si4702_read_reg(SI4702_STATUSRSSI, &si4702_reg_data);

		if (error_ind) {
			dev_err(&client->dev, "Failed to set read STC\n");
			return -1;
		}
		if ((si4702_reg_data & SI4702_STC_BIT) == 0)
			break;
	} while (++loop_counter < DELAY_WAIT);

	/* check loop_counter */
	if (loop_counter >= DELAY_WAIT) {
		dev_err(&client->dev, "Can't wait for STC bit clean");
		return -1;
	}
	dev_info(&client->dev, "loop counter %d\n", loop_counter);

	/* read RSSI */
	error_ind = si4702_read_reg(SI4702_READCHAN, &si4702_reg_data);

	if (error_ind) {
		dev_err(&client->dev, "Failed to read RSSI\n");
		return -1;
	}

	channel = si4702_reg_data & SI4702_CHAN_MASK;
	dev_info(&client->dev, "seek finish: channel(%d)\n", channel);

	return 0;
}

static s32 si4702_channel_seek(s16 dir)
{
	u16 loop_counter = 0;
	u16 si4702_reg_data, reg_power_cfg;
	u8 error_ind = 0;
	u32 channel, freq;
	struct i2c_client *client;
	struct si4702_info *info;

	if (NULL == si4702_drvdata)
		return -1;

	info = &si4702_drvdata->info;
	client = si4702_drvdata->client;

	error_ind = si4702_read_reg(SI4702_POWERCFG, &reg_power_cfg);

	if (info->mute) {
		/* check disable mute */
		reg_power_cfg &= ~SI4702_DMUTE_BIT;
	} else {
		reg_power_cfg |= SI4702_DMUTE_BIT;
	}

	if (dir) {
		reg_power_cfg |= SI4702_SEEKUP_BIT;
	} else {
		reg_power_cfg &= ~SI4702_SEEKUP_BIT;
	}
	/* start seek */
	reg_power_cfg |= SI4702_SEEK_BIT;
	error_ind = si4702_write_reg(SI4702_POWERCFG, reg_power_cfg);

	if (error_ind) {
		dev_err(&client->dev, "Failed to set seek start bit\n");
		return -1;
	}

	/* wait STC == 1 */
	do {
		error_ind =
		    si4702_read_reg(SI4702_STATUSRSSI, &si4702_reg_data);
		if (error_ind) {
			dev_err(&client->dev, "Failed to read STC bit\n");
			return -1;
		}

		if ((si4702_reg_data & SI4702_STC_BIT) != 0)
			break;
	} while (++loop_counter < DELAY_WAIT);

	/* clear seek bit */
	reg_power_cfg &= ~SI4702_SEEK_BIT;
	error_ind = si4702_write_reg(SI4702_POWERCFG, reg_power_cfg);
	if (error_ind) {
		dev_err(&client->dev, "Failed to stop seek\n");
		return -1;
	}

	if (loop_counter >= DELAY_WAIT) {
		dev_err(&client->dev, "Can't wait for STC bit set\n");
		return -1;
	}

	/* check whether SF==1 (seek failed bit) */
	if ((si4702_reg_data & SI4702_SF_BIT) != 0) {
		dev_err(&client->dev, "Failed to seek any channel\n");
		return -1;
	}

	loop_counter = 0;
	/* wait STC == 0 */
	do {
		error_ind =
		    si4702_read_reg(SI4702_STATUSRSSI, &si4702_reg_data);

		if (error_ind) {
			dev_err(&client->dev,
				"Failed to wait STC bit to clear\n");
			return -1;
		}
		if ((si4702_reg_data & SI4702_STC_BIT) == 0)
			break;
	} while (++loop_counter < DELAY_WAIT);

	/* check loop_counter */
	if (loop_counter >= DELAY_WAIT) {
		dev_err(&client->dev, "Can't wait for STC bit clean");
		return -1;
	}

	error_ind = si4702_read_reg(SI4702_READCHAN, &si4702_reg_data);

	if (error_ind) {
		dev_err(&client->dev, "I2C simulate failed\n");
		return -1;
	}

	channel = si4702_reg_data & SI4702_CHAN_MASK;
	freq = channel * info->space + info->min_band;
	dev_err(&client->dev,
		"seek finish: channel(%d), freq(%dKHz)\n", channel, freq);

	return 0;
}

static int si4702_startup(void)
{
	u16 magic = 0, id;
	struct i2c_client *client;
	struct mxc_fm_platform_data *data;

	if (NULL == si4702_drvdata)
		return -1;

	if (si4702_drvdata->vio)
		regulator_enable(si4702_drvdata->vio);
	if (si4702_drvdata->vdd)
		regulator_enable(si4702_drvdata->vdd);
	data = si4702_drvdata->plat_data;
	client = si4702_drvdata->client;

	/* read prior to write, otherwise write op will fail */
	si4702_read_reg(SI4702_DEVICEID, &id);
	dev_err(&client->dev, "si4702: DEVICEID: 0x%x\n", id);

	si4702_clock_en(1);
	msleep(100);

	/* disable mute, stereo, seek down, powerup */
	si4702_write_reg(SI4702_POWERCFG, SI4702_DMUTE_BIT | SI4702_ENABLE_BIT);
	msleep(500);
	si4702_read_reg(SI4702_TEST1, &magic);
	if (magic != 0x3C04)
		dev_err(&client->dev, "magic number 0x%x.\n", magic);
	/* close tune, set channel to 0 */
	si4702_write_reg(SI4702_CHANNEL, 0);
	/* disable interrupt, disable GPIO */
	si4702_write_reg(SI4702_SYSCONFIG1, 0);
	/* set volume to middle level */
	si4702_set_vol(0xf);

	si4702_set_space(data->space);
	si4702_set_band_range(data->band);
	si4702_set_seekth(data->seekth);
	si4702_set_skcnt(data->skcnt);
	si4702_set_sksnr(data->sksnr);

	return 0;
}

static void si4702_shutdown(void)
{
	if (NULL == si4702_drvdata)
		return;

	si4702_write_reg(SI4702_POWERCFG, SI4702_DMUTE_BIT |
			 SI4702_ENABLE_BIT | SI4702_DISABLE_BIT);
	msleep(100);
	si4702_clock_en(0);

	if (si4702_drvdata->vdd)
		regulator_disable(si4702_drvdata->vdd);
	if (si4702_drvdata->vio)
		regulator_disable(si4702_drvdata->vio);
}

enum {
	FM_STARTUP = 0,
	FM_SHUTDOWN,
	FM_RESET,
	FM_VOLUP,
	FM_VOLDOWN,
	FM_SEEK_UP,
	FM_SEEK_DOWN,
	FM_MUTEON,
	FM_MUTEDIS,
	FM_SEL,
	FM_SEEKTH,
	FM_DL,
	FM_CTL_MAX
};

static const char *const fm_control[FM_CTL_MAX] = {
	[FM_STARTUP] = "start",
	[FM_SHUTDOWN] = "halt",
	[FM_RESET] = "reset",
	[FM_VOLUP] = "volup",
	[FM_VOLDOWN] = "voldown",
	[FM_SEEK_UP] = "seeku",
	[FM_SEEK_DOWN] = "seekd",
	[FM_MUTEON] = "mute",
	[FM_MUTEDIS] = "muted",
	[FM_SEL] = "select",
	[FM_SEEKTH] = "seekth",
	[FM_DL] = "delay"
};

static int cmd(unsigned int index, int arg)
{
	struct i2c_client *client;
	struct mxc_fm_platform_data *plat_data;

	if (NULL == si4702_drvdata)
		return -1;

	client = si4702_drvdata->client;
	plat_data = si4702_drvdata->plat_data;

	switch (index) {
	case FM_SHUTDOWN:
		dev_err(&client->dev, "FM_SHUTDOWN\n");
		si4702_shutdown();
		break;
	case FM_STARTUP:
		dev_err(&client->dev, "FM_STARTUP\n");
		si4702_reset();
		si4702_startup();
		break;
	case FM_RESET:
		dev_err(&client->dev, "FM_RESET\n");
		si4702_reset();
		break;
	case FM_SEEK_DOWN:
		dev_err(&client->dev, "SEEK DOWN\n");
		si4702_channel_seek(0);
		break;
	case FM_SEEK_UP:
		dev_err(&client->dev, "SEEK UP\n");
		si4702_channel_seek(1);
		break;
	case FM_SEL:
		dev_err(&client->dev, "select %d\n", arg * 100);
		si4702_channel_select(arg * 100);
		break;
	case FM_SEEKTH:
		dev_err(&client->dev, "seekth = %d\n", arg);
		si4702_set_seekth(arg);
		break;
	case FM_DL:
		dev_err(&client->dev, "delay = %d\n", arg);
		break;
	default:
		dev_err(&client->dev, "error command\n");
		break;
	}
	return 0;
}

static ssize_t si4702_show(struct device *dev,
			   struct device_attribute *attr, char *buf)
{
	struct si4702_drvdata *drv_data = dev_get_drvdata(dev);
	u16 device_id;

	dev_err(&(drv_data->client->dev), "si4702 show\n");
	si4702_read_reg(SI4702_DEVICEID, &device_id);
	pr_info("device id %x\n", device_id);
	si4702_dump_reg();
	return 0;
}

static ssize_t si4702_store(struct device *dev,
			    struct device_attribute *attr, const char *buf,
			    size_t count)
{
	int state = 0;
	const char *const *s;
	char *p = NULL;
	int error;
	int len, arg = 0;
	struct si4702_drvdata *drv_data = dev_get_drvdata(dev);
	struct i2c_client *client = drv_data->client;

	dev_err(&client->dev, "si4702 store %d\n", count);

	p = memchr(buf, ' ', count);
	if (p) {
		len = p - buf;
		*p = '\0';
	} else
		len = count;

	len -= 1;
	dev_err(&client->dev, "cmd %s\n", buf);

	for (s = &fm_control[state]; state < FM_CTL_MAX; s++, state++) {
		if (*s && !strncmp(buf, *s, len)) {
			break;
		}
	}
	if (state < FM_CTL_MAX && *s) {
		if (p)
			arg = simple_strtoul(p + 1, NULL, 0);
		dev_err(&client->dev, "arg = %d\n", arg);
		error = cmd(state, arg);
	} else {
		dev_err(&client->dev, "error cmd\n");
		error = -EINVAL;
	}

	return error ? error : count;
}

static int ioctl_si4702(struct inode *inode, struct file *file,
			unsigned int cmd, unsigned long arg)
{
	void __user *argp = (void __user *)arg;
	int mute = 0;
	u16 data;
	int error;
	u8 volume;
	unsigned int freq;
	int dir;
	struct i2c_client *client;
	struct si4702_info *info;

	if (NULL == si4702_drvdata)
		return -1;

	info = &si4702_drvdata->info;
	client = si4702_drvdata->client;

	dev_err(&client->dev, "ioctl, cmd: 0x%x, arg: 0x%lx\n", cmd, arg);

	switch (cmd) {
	case SI4702_SETVOLUME:
		/* get volume from user */
		if (copy_from_user(&volume, argp, sizeof(u8))) {

			dev_err(&client->dev,
				"ioctl, copy volume value from user failed\n");
			return -EFAULT;
		}
		dev_err(&client->dev, "volume %d\n", volume);
		/* refill the register value */
		volume &= 0x0f;
		if (info->mute)
			error = si4702_write_reg(SI4702_POWERCFG, 0x0001);
		else
			error = si4702_write_reg(SI4702_POWERCFG, 0x4001);

		error = si4702_write_reg(SI4702_CHANNEL, 0);
		error = si4702_write_reg(SI4702_SYSCONFIG1, 0);
		error = si4702_write_reg(SI4702_SYSCONFIG2, 0x0f10 | volume);
		if (error) {
			dev_err(&client->dev, "ioctl, set volume failed\n");
			return -EFAULT;
		}
		/* renew the device info */
		info->volume = volume;

		break;
	case SI4702_GETVOLUME:
		/* just copy volume value to user */
		if (copy_to_user(argp, &(info->volume), sizeof(unsigned int))) {
			dev_err(&client->dev, "ioctl, copy to user failed\n");
			return -EFAULT;
		}
		break;
	case SI4702_MUTEON:
		mute = 1;
	case SI4702_MUTEOFF:
		if (mute) {
			/* enable mute */
			si4702_read_reg(SI4702_POWERCFG, &data);
			data &= 0x00FF;
			error = si4702_write_reg(SI4702_POWERCFG, data);
		} else {
			si4702_read_reg(SI4702_POWERCFG, &data);
			data &= 0x00FF;
			data |= 0x4000;
			error = si4702_write_reg(SI4702_POWERCFG, data);
		}
		if (error) {
			dev_err(&client->dev, "ioctl, set mute failed\n");
			return -EFAULT;
		}
		break;
	case SI4702_SELECT:
		if (copy_from_user(&freq, argp, sizeof(unsigned int))) {

			dev_err(&client->dev,
				"ioctl, copy frequence from user failed\n");
			return -EFAULT;
		}
		/* check frequence */
		if (freq > info->max_band || freq < info->min_band) {
			dev_err(&client->dev,
				"the frequence select is out of band\n");
			return -EINVAL;
		}
		if (si4702_channel_select(freq)) {
			dev_err(&client->dev,
				"ioctl, failed to select channel\n");
			return -EFAULT;
		}
		break;
	case SI4702_SEEK:
		if (copy_from_user(&dir, argp, sizeof(int))) {

			dev_err(&client->dev, "ioctl, copy from user failed\n");
			return -EFAULT;
		}
		/* get seeked channel */
		dir = si4702_channel_seek(dir);
		if (dir == -1) {
			return -EAGAIN;
		} else if (dir == -2) {
			return -EFAULT;
		}
		if (copy_to_user(argp, &dir, sizeof(int))) {

			dev_err(&client->dev,
				"ioctl, copy seek frequnce to user failed\n");
			return -EFAULT;
		}
		break;
	default:
		dev_err(&client->dev, "SI4702: Invalid ioctl command\n");
		return -EINVAL;

	}
	return 0;
}

static int open_si4702(struct inode *inode, struct file *file)
{
	struct i2c_client *client;

	if (NULL == si4702_drvdata)
		return -1;

	client = si4702_drvdata->client;

	spin_lock(&count_lock);
	if (si4702_drvdata->count != 0) {
		dev_err(&client->dev, "device has been open already\n");
		spin_unlock(&count_lock);
		return -EBUSY;
	}
	si4702_drvdata->count++;
	spin_unlock(&count_lock);

	/* request and active GPIO */
	si4702_gpio_get();
	/* reset the si4702 from it's reset pin */
	si4702_reset();

	/* startup si4702 */
	if (si4702_startup()) {
		spin_lock(&count_lock);
		si4702_drvdata->count--;
		spin_unlock(&count_lock);
		return -ENODEV;
	}

	return 0;
}

static int release_si4702(struct inode *inode, struct file *file)
{
	struct i2c_client *client;

	if (NULL == si4702_drvdata)
		return -1;

	client = si4702_drvdata->client;

	dev_err(&client->dev, "release\n");
	/* software shutdown */
	si4702_shutdown();
	/* inactive, free GPIO, cut power */
	si4702_gpio_put();

	spin_lock(&count_lock);
	si4702_drvdata->count--;
	spin_unlock(&count_lock);

	return 0;
}

static int si4702_suspend(struct i2c_client *client, pm_message_t state)
{
	return 0;
}

static int si4702_resume(struct i2c_client *client)
{
	return 0;
}

static struct device_attribute si4702_dev_attr = {
	.attr = {
		 .name = "si4702_ctl",
		 .mode = S_IRUSR | S_IWUSR,
		 },
	.show = si4702_show,
	.store = si4702_store,
};

static struct file_operations si4702_fops = {
	.owner = THIS_MODULE,
	.open = open_si4702,
	.release = release_si4702,
	.ioctl = ioctl_si4702,
};

static int __devinit si4702_probe(struct i2c_client *client,
				  const struct i2c_device_id *id)
{
	int ret = 0;
	struct mxc_fm_platform_data *plat_data;
	struct si4702_drvdata *drv_data;
	struct device *dev;

	dev_info(&client->dev, "si4702 device probe process start.\n");

	plat_data = (struct mxc_fm_platform_data *)client->dev.platform_data;
	if (plat_data == NULL) {
		dev_err(&client->dev, "lack of platform data!\n");
		return -ENODEV;
	}

	drv_data = kmalloc(sizeof(struct si4702_drvdata), GFP_KERNEL);
	if (drv_data == NULL) {
		dev_err(&client->dev, "lack of kernel memory!\n");
		return -ENOMEM;
	}
	memset(drv_data, 0, sizeof(struct si4702_drvdata));
	drv_data->plat_data = plat_data;
	drv_data->major = DEV_MAJOR;
	drv_data->minor = DEV_MINOR;
	drv_data->count = 0;

	/*enable power supply */
	if (plat_data->reg_vio != NULL) {
		drv_data->vio = regulator_get(&client->dev, plat_data->reg_vio);
		if (drv_data->vio == ERR_PTR(-ENOENT))
			goto free_drv_data;
		regulator_enable(drv_data->vio);
	}

	/* here, we assume that vio and vdd are not the same */
	if (plat_data->reg_vdd != NULL) {
		drv_data->vdd = regulator_get(&client->dev, plat_data->reg_vdd);
		if (drv_data->vdd == ERR_PTR(-ENOENT))
			goto disable_vio;
		regulator_enable(drv_data->vdd);
	}

	/*attach client and check device id */
	if (SI4702_DEVICE_ID != si4702_id_detect(client)) {
		dev_err(&client->dev, "id wrong.\n");
		goto disable_vdd;
	}
	dev_info(&client->dev, "chip id %x detect.\n", SI4702_DEVICE_ID);
	drv_data->client = client;

	/*user interface begain */
	/*create device file in sysfs as a user interface,
	 * also for debug support */
	ret = device_create_file(&client->dev, &si4702_dev_attr);
	if (ret) {
		dev_err(&client->dev, "create device file failed!\n");
		goto gpio_put;	/* shall i use some meanful error code? */
	}

	/*create a char dev for application code access */
	if (drv_data->major) {
		ret = register_chrdev(drv_data->major, "si4702", &si4702_fops);;
	} else {
		ret = register_chrdev(0, "si4702", &si4702_fops);
	}

	if (drv_data->major == 0)
		drv_data->major = ret;

	/* create class and device for udev information */
	drv_data->radio_class = class_create(THIS_MODULE, "radio");
	if (IS_ERR(drv_data->radio_class)) {
		dev_err(&client->dev, "SI4702: failed to create radio class\n");
		goto char_dev_remove;
	}

	dev = device_create(drv_data->radio_class, NULL,
			    MKDEV(drv_data->major, drv_data->minor), NULL,
			    "si4702");
	if (IS_ERR(dev)) {
		dev_err(&client->dev,
			"SI4702: failed to create radio class device\n");
		goto class_remove;
	}
	/*User interface end */
	dev_set_drvdata(&client->dev, drv_data);
	si4702_drvdata = drv_data;

	si4702_gpio_get();
	dev_info(&client->dev, "si4702 device probe successfully.\n");
	si4702_shutdown();

	return 0;

class_remove:
	class_destroy(drv_data->radio_class);
char_dev_remove:
	unregister_chrdev(drv_data->major, "si4702");
	device_remove_file(&client->dev, &si4702_dev_attr);
gpio_put:
	si4702_gpio_put();
disable_vdd:
	if (plat_data->reg_vdd) {
		regulator_disable(drv_data->vdd);
		regulator_put(drv_data->vdd);
	}
disable_vio:
	if (plat_data->reg_vio) {
		regulator_disable(drv_data->vio);
		regulator_put(drv_data->vio);
	}

free_drv_data:
	kfree(drv_data);

	return -ENODEV;
}

static int __devexit si4702_remove(struct i2c_client *client)
{
	struct mxc_fm_platform_data *plat_data;
	struct si4702_drvdata *drv_data = dev_get_drvdata(&client->dev);

	plat_data = (struct mxc_fm_platform_data *)client->dev.platform_data;

	device_destroy(drv_data->radio_class,
		       MKDEV(drv_data->major, drv_data->minor));
	class_destroy(drv_data->radio_class);

	unregister_chrdev(drv_data->major, "si4702");
	device_remove_file(&client->dev, &si4702_dev_attr);
	si4702_gpio_put();

	if (plat_data->reg_vdd)
		regulator_put(drv_data->vdd);

	if (plat_data->reg_vio)
		regulator_put(drv_data->vio);

	kfree(si4702_drvdata);
	si4702_drvdata = NULL;

	return 0;
}

static const struct i2c_device_id si4702_id[] = {
	{"si4702", 0},
	{},
};

MODULE_DEVICE_TABLE(i2c, si4702_id);

static struct i2c_driver i2c_si4702_driver = {
	.driver = {
		   .name = "si4702",
		   },
	.probe = si4702_probe,
	.remove = si4702_remove,
	.suspend = si4702_suspend,
	.resume = si4702_resume,
	.id_table = si4702_id,
};

static int __init init_si4702(void)
{
	/*add to i2c driver */
	pr_info("add si4702 i2c driver\n");
	return i2c_add_driver(&i2c_si4702_driver);
}

static void __exit exit_si4702(void)
{
	i2c_del_driver(&i2c_si4702_driver);
}

module_init(init_si4702);
module_exit(exit_si4702);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("SI4702 FM driver");
MODULE_LICENSE("GPL");