support different wifi bt chip auto compatible
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / rockchip_wlan / rda5990 / drv_fm_rda / RDA5990_FM_drv.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/delay.h> // udelay()
5 #include <linux/device.h> // device_create()
6 #include <linux/platform_device.h>
7 #include <linux/i2c.h>
8 #include <linux/cdev.h>
9 #include <linux/fs.h>
10 #include <linux/version.h>      /* constant of kernel version */
11 #include <asm/uaccess.h> // get_user()
12
13 #include <linux/fm.h>
14 #include <mach/mt6575_gpio.h>
15 #include <mach/mtk_rtc.h>
16 #include <linux/proc_fs.h>
17 #include <linux/string.h>
18 #include <linux/delay.h>
19
20 // if need debug, define FMDEBUG
21 //#define FMDEBUG
22
23 // if your platform is MT6515/6575, define MTK_MT6515 
24 #define MTK_MT6515
25
26 // if your platform is MT6515/6575 and MTK FM is MT6626, define MT6626 
27 //#define MT6626
28
29 #define FM_ALERT(f, s...) \
30         do { \
31                 printk(KERN_ALERT "RDAFM " f, ## s); \
32         } while(0)
33
34 #ifdef FMDEBUG
35 #define FM_DEBUG(f, s...) \
36         do { \
37                 printk("RDAFM " f, ## s); \
38         } while(0)
39 #else
40 #define FM_DEBUG(f, s...)
41 #endif
42
43 #define RDA599X_SCANTBL_SIZE  16 //16*uinit16_t
44 #define RDA599X_FM_SCAN_UP       0x0
45 #define RDA599X_FM_SCAN_DOWN     0x01
46
47 extern int rda_fm_power_off();
48 extern int rda_fm_power_on();
49
50 /******************************************************************************
51  * CONSTANT DEFINITIONS
52  *****************************************************************************/
53 #define RDAFM_SLAVE_ADDR        (0x11 << 1)    //RDA FM Chip address
54
55 #define RDAFM_MASK_RSSI         0X7F // RSSI
56 #define RDAFM_DEV               "RDA599x"
57
58 //customer need customize the I2C port
59 #define RDAFM_I2C_PORT          0
60
61
62 #define ID_RDA5802E             0x5804
63 #define ID_RDA5802H             0x5801
64 #define ID_RDA5802N             0x5808
65 #define ID_RDA5820              0x5805
66 #define ID_RDA5820NS            0x5820
67
68
69 static struct proc_dir_entry *g_fm_proc = NULL;
70 static struct fm *g_fm_struct = NULL;
71 static atomic_t scan_complete_flag;
72
73 #define FM_PROC_FILE "fm"
74
75 /******************************************************************************
76  * STRUCTURE DEFINITIONS
77  *****************************************************************************/
78
79 enum RDAFM_CHIP_TYPE {
80         CHIP_TYPE_RDA5802E = 0,
81         CHIP_TYPE_RDA5802H,
82         CHIP_TYPE_RDA5802N,
83         CHIP_TYPE_RDA5820,
84         CHIP_TYPE_RDA5820NS,
85 };
86
87
88 typedef struct
89 {
90         uint8_t         address;
91         uint16_t        value;
92 }RDA_FM_REG_T;   
93
94 typedef struct
95 {
96         bool            byPowerUp;
97         struct fm_tune_parm parm
98 }FM_TUNE_T;
99 static FM_TUNE_T fm_tune_data = {false, {}};
100
101 typedef enum
102 {
103         FM_RECEIVER,                            //5800,5802,5804
104         FM_TRANSMITTER,                 //5820
105 }RDA_RADIO_WORK_E;
106
107 typedef enum
108 {
109         OFF,
110         ON,
111 }RDA_FM_POWER_STATE_T;
112
113 struct fm {
114         uint32_t ref;
115         bool powerup;
116         uint16_t chip_id;
117         uint16_t device_id;
118         dev_t dev_t;
119         uint16_t min_freq; // KHz
120         uint16_t max_freq; // KHz
121         uint8_t band;   // TODO
122         struct class *cls;
123         struct device *dev;
124         struct cdev cdev;
125         struct i2c_client *i2c_client;
126 };
127
128
129
130
131 /******************************************************************************
132  * FUNCTION PROTOTYPES
133  *****************************************************************************/
134
135 static int RDAFM_clear_hmute(struct i2c_client *client);
136 static int RDAFM_enable_hmute(struct i2c_client *client);
137 static int RDAFM_clear_tune(struct i2c_client *client);
138 static int RDAFM_enable_tune(struct i2c_client *client);
139 static int RDAFM_clear_seek(struct i2c_client *client);
140 static int RDAFM_enable_seek(struct i2c_client *client);
141 static int RDAFM_SetStereo(struct i2c_client *client,uint8_t b);
142 static int RDAFM_SetRSSI_Threshold(struct i2c_client *client,uint8_t RssiThreshold);
143 static int RDAFM_SetDe_Emphasis(struct i2c_client *client,uint8_t index);
144 static bool RDAFM_Scan(struct i2c_client *client,
145                 uint16_t min_freq, uint16_t max_freq,
146                 uint16_t *pFreq, //get the valid freq after scan
147                 uint16_t *pScanTBL,
148                 uint16_t *ScanTBLsize,
149                 uint16_t scandir,
150                 uint16_t space);
151
152
153 static int RDAFM_read(struct i2c_client *client, uint8_t addr, uint16_t *val);
154 static int RDAFM_write(struct i2c_client *client, uint8_t addr, uint16_t val);
155 static void RDAFM_em_test(struct i2c_client *client, uint16_t group_idx, uint16_t item_idx, uint32_t item_value);
156 static int fm_setup_cdev(struct fm *fm);
157 static int fm_ops_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
158 static loff_t fm_ops_lseek(struct file *filp, loff_t off, int whence);
159 static int fm_ops_open(struct inode *inode, struct file *filp);
160 static int fm_ops_release(struct inode *inode, struct file *filp);
161
162 static int fm_init(struct i2c_client *client);
163 static int fm_destroy(struct fm *fm);
164 static int fm_powerup(struct fm *fm, struct fm_tune_parm *parm);
165 static int fm_powerdown(struct fm *fm);
166
167 static int fm_tune(struct fm *fm, struct fm_tune_parm *parm);
168 static int fm_seek(struct fm *fm, struct fm_seek_parm *parm);
169 static int fm_scan(struct fm *fm, struct fm_scan_parm *parm);
170 static int fm_setvol(struct fm *fm, uint32_t vol);
171 static int fm_getvol(struct fm *fm, uint32_t *vol);
172 static int fm_getrssi(struct fm *fm, uint32_t *rssi);
173 static int fm_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data);
174
175 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31))
176 static int fm_i2c_attach_adapter(struct i2c_adapter *adapter);
177 static int fm_i2c_detect(struct i2c_adapter *adapter, int addr, int kind);
178 static int fm_i2c_detach_client(struct i2c_client *client);
179 #else
180 static int fm_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
181 static int fm_i2c_detect(struct i2c_client *client, int kind, struct i2c_board_info *info);
182 static int fm_i2c_remove(struct i2c_client *client);
183 #endif
184
185 /******************************************************************************
186  * GLOBAL DATA
187  *****************************************************************************/
188 /* Addresses to scan */
189 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31))
190 static unsigned short normal_i2c[] = {RDAFM_SLAVE_ADDR, I2C_CLIENT_END};
191 static unsigned short ignore = I2C_CLIENT_END;
192
193 static struct i2c_client_address_data RDAFM_addr_data = {
194         .normal_i2c = normal_i2c,
195         .probe = &ignore,
196         .ignore = &ignore,
197 };
198 #else
199 static const struct i2c_device_id fm_i2c_id = {RDAFM_DEV, 0};
200 static unsigned short force[] = {RDAFM_I2C_PORT, RDAFM_SLAVE_ADDR, I2C_CLIENT_END, I2C_CLIENT_END};
201 static const unsigned short * const forces[] = {force, NULL};
202 //static struct i2c_client_address_data addr_data = {.forces = forces};
203 static struct i2c_board_info __initdata i2c_rdafm={ I2C_BOARD_INFO(RDAFM_DEV, (RDAFM_SLAVE_ADDR>>1))};
204 #endif
205
206         static struct i2c_driver RDAFM_driver = {
207 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31))
208                 .driver = {
209                         .owner = THIS_MODULE,
210                         .name = RDAFM_DEV,
211                 },
212                 .attach_adapter = fm_i2c_attach_adapter,
213                 .detach_client = fm_i2c_detach_client,
214 #else
215                 .probe = fm_i2c_probe,
216                 .remove = fm_i2c_remove,
217                 .detect = fm_i2c_detect,
218                 .driver.name = RDAFM_DEV,
219                 .id_table = &fm_i2c_id,
220         //      .address_data = &addr_data,
221                 .address_list = (const unsigned short*) forces,
222 #endif
223         };
224
225 static uint16_t RDAFM_CHIP_ID = 0x5808;
226 static RDA_RADIO_WORK_E RDA_RADIO_WorkType = FM_RECEIVER;
227
228
229
230 #if 1
231 static uint16_t RDA5802N_initialization_reg[]={
232         0xC005, //02h 
233         0x0000,
234         0x0400,
235         0xC6ED, //0x86AD, //05h
236         0x6000,
237         0x721A, //0x42C6
238         0x0000,
239         0x0000,
240         0x0000,  //0x0ah
241         0x0000,
242         0x0000,
243         0x0000,
244         0x0000,
245         0x0000,
246         0x0000,  //0x10h
247         0x0019,
248         0x2A11,
249         0xB042,  
250         0x2A11,  
251         0xB831,  //0x15h 
252         0xC000,
253         0x2A91,
254         0x9400,
255         0x00A8,
256         0xc400,  //0x1ah
257         0xF7CF,  //Ìá¸ßÔ¶¶ËÔëÉùÒÖÖÆ  
258         0x2414, //0x2ADC,  //0x1ch ÌáÉýVIO VDDÖ®¼äѹ²îÒýÆðµÄ²»Á¼
259         0x806F, 
260         0x4608,
261         0x0086,
262         0x0661, //0x20H
263         0x0000,
264         0x109E,
265         0x23C8,
266         0x0406,
267         0x0E1C, //0x25H
268 };
269 #else
270 static uint16_t RDA5802N_initialization_reg[]={
271         0xc401, //02h
272         0x0000,
273         0x0400,
274         0x86ad, //05h//
275         0x0000,
276         0x42c6,
277         0x0000,
278         0x0000,
279         0x0000,  //0x0ah
280         0x0000,
281         0x0000,
282         0x0000,
283         0x0000,
284         0x0000,
285         0x0000,  //0x10h
286         0x0019,  
287         0x2a11,
288         0xa053,//0x80,0x53,
289         0x3e11,//0x22,0x11,     
290         0xfc7d,  //0x15h 
291         0xc000,
292         0x2a91,
293         0x9400,
294         0x00a8,
295         0xc400,  //0x1ah
296         0xe000,
297         0x2b1d, //0x23,0x14
298         0x816a,
299         0x4608,
300         0x0086,
301         0x0661,  //0x20h
302         0x0000,  
303         0x109e,
304         0x2244,
305         0x0408,  //0x24
306         0x0408,  //0x25
307 };
308 #endif
309
310 static RDA_FM_REG_T RDA5820NS_TX_initialization_reg[]={
311         {0x02, 0xE003},
312         {0xFF, 100},    // if address is 0xFF, sleep value ms
313         {0x02, 0xE001},
314         {0x19, 0x88A8},
315         {0x1A, 0x4290},
316         {0x68, 0x0AF0},
317         {0x40, 0x0001},
318         {0x41, 0x41FF},
319         {0xFF, 500},
320         {0x03, 0x1B90},
321 };
322
323 static RDA_FM_REG_T RDA5820NS_RX_initialization_reg[]={
324         {0x02, 0x0002}, //Soft reset
325         {0xFF, 100},    // wait
326         {0x02, 0xC001},  //Power Up 
327         {0x05, 0x888F},  //LNAP  0x884F --LNAN
328         {0x06, 0x6000},
329         {0x13, 0x80E1},
330         {0x14, 0x2A11},
331         {0x1C, 0x22DE},
332         {0x21, 0x0020},
333         {0x03, 0x1B90},
334 };
335
336
337
338 static struct file_operations fm_ops = {
339         .owner = THIS_MODULE,
340         .unlocked_ioctl = fm_ops_ioctl,
341         .llseek = fm_ops_lseek,
342         .open = fm_ops_open,
343         .release = fm_ops_release,
344 };
345
346 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
347 static DECLARE_MUTEX(fm_ops_mutex);
348 #else
349 DEFINE_SEMAPHORE(fm_ops_mutex);
350 #endif
351
352 /******************************************************************************
353  *****************************************************************************/
354
355 /******************************************************************************
356  *****************************************************************************/
357
358
359
360 static int RDAFM_GetChipID(struct i2c_client *client, uint16_t *pChipID)
361 {
362         int err;
363         int ret = -1;
364         uint16_t val = 0x0002;
365
366         //Reset RDA FM
367         err = RDAFM_write(client, 0x02, val);
368         if(err < 0){
369 #ifdef FMDEBUG
370                 FM_DEBUG("RDAFM_GetChipID: reset FM chip failed!\n");
371 #endif
372                 ret = -1;
373                 return ret;
374         }
375         msleep(80);
376
377         val = 0;
378         err = RDAFM_read(client, 0x0C, &val);
379         if (err == 0)
380         {
381                 if ((0x5802 == val) || (0x5803 == val))
382                 {
383                         err = RDAFM_read(client, 0x0E, &val);
384
385                         if (err == 0)
386                                 *pChipID = val;
387                         else
388                                 *pChipID = 0x5802;
389
390 #ifdef FMDEBUG
391                         FM_DEBUG("RDAFM_GetChipID: Chip ID = %04X\n", val);
392 #endif
393
394                         ret = 0;
395
396                 }
397                 else if ((0x5805 == val) || (0x5820 == val))
398                 {
399                         *pChipID = val;
400                         ret = 0;
401                 }
402                 else
403                 {
404 #ifdef FMDEBUG
405                         FM_DEBUG("RDAFM_GetChipID: get chip ID failed! get value = %04X\n", val);
406 #endif
407                         ret = -1;
408                 }
409
410         }
411         else
412         {
413 #ifdef FMDEBUG
414                 FM_DEBUG("RDAFM_GetChipID: get chip ID failed!\n");
415 #endif
416                 ret = -1;
417         }
418
419         return ret;
420 }
421
422
423 /*
424  *  RDAFM_read
425  */
426 static int RDAFM_read(struct i2c_client *client, uint8_t addr, uint16_t *val)
427 {
428         int n;
429         char b[2] = {0};
430
431         // first, send addr to RDAFM
432         n = i2c_master_send(client, (char*)&addr, 1);
433         if (n < 0)
434         {
435                 FM_ALERT("RDAFM_read send:0x%X err:%d\n", addr, n);
436                 return -1;
437         }
438
439         // second, receive two byte from RDAFM
440         n = i2c_master_recv(client, b, 2);
441         if (n < 0)
442         {
443                 FM_ALERT("RDAFM_read recv:0x%X err:%d\n", addr, n);
444                 return -1;
445         }
446
447         *val = (uint16_t)(b[0] << 8 | b[1]);
448
449         return 0;
450 }
451
452 /*
453  *  RDAFM_write
454  */
455 static int RDAFM_write(struct i2c_client *client, uint8_t addr, uint16_t val)
456 {
457         int n;
458         char b[3];
459
460         b[0] = addr;
461         b[1] = (char)(val >> 8);
462         b[2] = (char)(val & 0xFF);
463
464         n = i2c_master_send(client, b, 3);
465         if (n < 0)
466         {
467                 FM_ALERT("RDAFM_write send:0x%X err:%d\n", addr, n);
468                 return -1;
469         }
470
471         return 0;
472 }
473
474
475 static int RDAFM_clear_hmute(struct i2c_client *client)
476 {
477         int ret = 0;
478         uint16_t tRegValue = 0;
479
480         FM_DEBUG("RDAFM_clear_hmute\n");
481
482         ret = RDAFM_read(client, 0x02, &tRegValue);
483         if (ret < 0)
484         {
485                 FM_ALERT("RDAFM_clear_hmute  read register failed!\n"); 
486                 return -1;
487         }
488
489         tRegValue |= (1 << 14);
490
491         ret = RDAFM_write(client, 0x02, tRegValue);
492
493         if (ret < 0)
494         {
495                 FM_ALERT("RDAFM_clear_hmute  write register failed!\n"); 
496                 return -1;
497         }
498
499         if(fm_tune_data.byPowerUp){
500                 if (fm_tune(g_fm_struct, &(fm_tune_data.parm)) < 0)
501                 {
502                         fm_tune_data.byPowerUp = false;
503                         memset(&fm_tune_data.parm, 0, sizeof(fm_tune_data.parm));
504                         return -EPERM;
505                 }
506                 fm_tune_data.byPowerUp = false;
507                 memset(&fm_tune_data.parm, 0, sizeof(fm_tune_data.parm));
508         }
509
510         return 0;
511 }
512
513
514
515 static int RDAFM_enable_hmute(struct i2c_client *client)
516 {
517         int ret = 0;
518         uint16_t tRegValue = 0;
519
520         FM_DEBUG("RDAFM_enable_hmute\n");
521
522         ret = RDAFM_read(client, 0x02, &tRegValue);
523         if (ret < 0)
524         {
525                 FM_ALERT("RDAFM_enable_hmute  read register failed!\n"); 
526                 return -1;
527         }
528
529         tRegValue &= (~(1 << 14));
530
531         ret = RDAFM_write(client, 0x02, tRegValue);
532
533         if (ret < 0)
534         {
535                 FM_ALERT("RDAFM_enable_hmute  write register failed!\n"); 
536                 return -1;
537         }
538
539         return 0;
540 }
541
542
543
544 static int RDAFM_clear_tune(struct i2c_client *client)
545 {
546         //Don't need it
547         return 0;
548 }
549
550
551
552 static int RDAFM_enable_tune(struct i2c_client *client)
553 {
554         //Don't need it
555         return 0;
556 }
557
558
559
560 static int RDAFM_clear_seek(struct i2c_client *client)
561 {
562         //Don't need it
563         return 0;
564 }
565
566
567
568 static int RDAFM_enable_seek(struct i2c_client *client)
569 {
570         //Don't need it
571         return 0;
572 }
573
574
575 //b=true set stereo else set mono
576 static int RDAFM_SetStereo(struct i2c_client *client, uint8_t b)
577 {
578         int ret = 0;
579         uint16_t tRegValue = 0;
580
581         FM_DEBUG("RDAFM_SetStereo\n");
582
583         ret = RDAFM_read(client, 0x02, &tRegValue);
584         if (ret < 0)
585         {
586                 FM_ALERT("RDAFM_SetStereo  read register failed!\n"); 
587                 return -1;
588         }
589         if (b)
590                 tRegValue &= (~(1 << 13));//set stereo
591         else
592                 tRegValue |= (1 << 13); //set mono
593
594         ret = RDAFM_write(client, 0x02, tRegValue);
595
596         if (ret < 0)
597         {
598                 FM_ALERT("RDAFM_SetStereo  write register failed!\n"); 
599                 return -1;
600         }
601
602
603         return 0;
604
605 }
606
607
608 static int RDAFM_SetRSSI_Threshold(struct i2c_client *client, uint8_t RssiThreshold)
609 {
610         int ret = 0;
611         uint16_t tRegValue = 0;
612
613         FM_DEBUG("RDAFM_SetRSSI_Threshold\n");
614
615         ret = RDAFM_read(client, 0x05, &tRegValue);
616         if (ret < 0)
617         {
618                 FM_ALERT("RDAFM_SetRSSI_Threshold  read register failed!\n"); 
619                 return -1;
620         }
621
622         tRegValue &= 0x80FF;//clear valume
623         tRegValue |= ((RssiThreshold & 0x7f) << 8); //set valume
624
625         ret = RDAFM_write(client, 0x05, tRegValue);
626
627         if (ret < 0)
628         {
629                 FM_ALERT("RDAFM_SetRSSI_Threshold  write register failed!\n"); 
630                 return -1;
631         }
632
633
634         return 0;
635
636 }
637
638
639
640 static int RDAFM_SetDe_Emphasis(struct i2c_client *client, uint8_t index)
641 {
642         int ret = 0;
643         uint16_t tRegValue = 0;
644
645         FM_DEBUG("RDAFM_SetRSSI_Threshold\n");
646
647         ret = RDAFM_read(client, 0x04, &tRegValue);
648         if (ret < 0)
649         {
650                 FM_ALERT("RDAFM_SetRSSI_Threshold  read register failed!\n"); 
651                 return -1;
652         }
653
654         if (0 == index)
655         {
656                 tRegValue &= (~(1 << 11));//De_Emphasis=75us
657         }
658         else if (1 == index)
659         {
660                 tRegValue |= (1 << 11);//De_Emphasis=50us
661         }
662
663
664         ret = RDAFM_write(client, 0x04, tRegValue);
665
666         if (ret < 0)
667         {
668                 FM_ALERT("RDAFM_SetRSSI_Threshold  write register failed!\n"); 
669                 return -1;
670         }
671
672
673         return 0;
674
675
676 }
677
678
679 static void RDAFM_em_test(struct i2c_client *client, uint16_t group_idx, uint16_t item_idx, uint32_t item_value)
680 {
681         FM_ALERT("RDAFM_em_test  %d:%d:%d\n", group_idx, item_idx, item_value); 
682         switch (group_idx)
683         {
684                 case mono:
685                         if(item_value == 1)
686                         {
687                                 RDAFM_SetStereo(client, 0); //force mono
688                         }
689                         else
690                         {
691                                 RDAFM_SetStereo(client, 1); //stereo
692
693                         }
694
695                         break;
696                 case stereo:
697                         if(item_value == 0)
698                         {
699                                 RDAFM_SetStereo(client, 1); //stereo
700                         }
701                         else
702                         {
703                                 RDAFM_SetStereo(client, 0); //force mono        
704                         }
705                         break;
706                 case RSSI_threshold:
707                         item_value &= 0x7F;
708                         RDAFM_SetRSSI_Threshold(client, item_value);
709                         break;              
710                 case Softmute_Enable:
711                         if (item_idx)
712                         {
713                                 RDAFM_enable_hmute(client);
714                         }
715                         else
716                         {
717                                 RDAFM_clear_hmute(client);
718                         }
719                         break;
720                 case De_emphasis:
721                         if(item_idx >= 2) //0us
722                         {
723                                 FM_ALERT("RDAFM not support De_emphasis 0\n");          
724                         }
725                         else
726                         {
727                                 RDAFM_SetDe_Emphasis(client,item_idx);//0=75us,1=50us
728                         }
729                         break;
730
731                 case HL_Side:
732
733                         break;
734                 default:
735                         FM_ALERT("RDAFM not support this setting\n");
736                         break;   
737         }
738 }
739
740 static bool RDAFM_Scan(struct i2c_client *client, 
741                 uint16_t min_freq, uint16_t max_freq,
742                 uint16_t *pFreq,
743                 uint16_t *pScanTBL, 
744                 uint16_t *ScanTBLsize, 
745                 uint16_t scandir, 
746                 uint16_t space)
747 {
748         uint16_t tFreq, tRegValue = 0;
749         uint16_t tmp_scanTBLsize = *ScanTBLsize;
750         int ret = -1;
751         bool isTrueStation = false;
752         uint16_t oldValue = 0;
753         int channel = 0;
754
755         if((!pScanTBL) || (tmp_scanTBLsize == 0)) {
756                 return false;
757         }
758
759         //clear the old value of pScanTBL
760         memset(pScanTBL, 0, sizeof(uint16_t)*RDA599X_SCANTBL_SIZE);
761
762         if(tmp_scanTBLsize > RDA599X_SCANTBL_SIZE)
763         {
764                 tmp_scanTBLsize = RDA599X_SCANTBL_SIZE;
765         }
766
767         //scan up
768         if(scandir == RDA599X_FM_SCAN_UP){ // now, only support scan up
769                 tFreq = min_freq;
770         }else{ //scan down
771                 tFreq = max_freq;//max_freq compare need or not   
772         }
773
774         //mute FM
775         RDAFM_enable_hmute(client);
776
777         //set seekth
778         tRegValue = 0;
779         RDAFM_read(client, 0x05, &tRegValue);
780         tRegValue &= (~(0x7f<<8));
781         tRegValue |= ((0x8 & 0x7f) << 8);
782         RDAFM_write(client, 0x05, tRegValue);
783         msleep(50);
784
785         atomic_set(&scan_complete_flag, 1);
786         do {
787                 if(atomic_read(&scan_complete_flag) == 0)
788                         break;
789                 isTrueStation = false;
790
791                 //set channel and enable TUNE
792                 tRegValue = 0;
793                 RDAFM_read(client, 0x03, &tRegValue);
794                 tRegValue &= (~(0x03ff<<6)); //clear bit[15:6]
795                 channel = tFreq - min_freq; 
796                 tRegValue |= ((channel << 6) | (1 << 4)); //set bit[15:6] and bit[4]
797                 ret = RDAFM_write(client, 0x03, tRegValue);
798                 msleep(40);
799
800                 //read 0x0B and check FM_TRUE(bit[8])
801                 tRegValue = 0;
802                 ret = RDAFM_read(client, 0x0B, &tRegValue);
803                 if(!ret){
804                         if((tRegValue & 0x0100) == 0x0100){
805                                 isTrueStation = true;
806                         }
807                 }
808
809                 //if this freq is a true station, read the channel
810                 if(isTrueStation){
811                         //tRegValue = 0;
812                         //RDAFM_read(client, 0x03, &tRegValue);
813                         //channel = ((tRegValue>>6) & 0x03ff) - 5;
814                         channel = channel - 5;
815                         if((channel >= 0) && (channel != 85)){
816                                 oldValue = *(pScanTBL+(channel/16));
817                                 oldValue |= (1<<(channel%16));
818                                 *(pScanTBL+(channel/16)) = oldValue;
819                         }
820                 }
821
822                 //increase freq
823                 tFreq += space;
824         }while( tFreq <= max_freq );
825
826 #if defined(MTK_MT6515) && defined(MT6626)
827         *(pScanTBL+13) = 0xb2d4;
828         *(pScanTBL+14) = 0xb2d4;
829         *(pScanTBL+15) = 0xb2d4;
830 #endif
831
832         *ScanTBLsize = tmp_scanTBLsize;
833         *pFreq = 0;
834
835         //clear FM mute
836         RDAFM_clear_hmute(client);
837
838         return true;
839 }
840
841
842 static int fm_setup_cdev(struct fm *fm)
843 {
844         int err;
845
846         err = alloc_chrdev_region(&fm->dev_t, 0, 1, FM_NAME);
847         if (err) {
848                 FM_ALERT("alloc dev_t failed\n");
849                 return -1;
850         }
851
852         FM_ALERT("alloc %s:%d:%d\n", FM_NAME,
853                         MAJOR(fm->dev_t), MINOR(fm->dev_t));
854
855         cdev_init(&fm->cdev, &fm_ops);
856
857         fm->cdev.owner = THIS_MODULE;
858         fm->cdev.ops = &fm_ops;
859
860         err = cdev_add(&fm->cdev, fm->dev_t, 1);
861         if (err) {
862                 FM_ALERT("alloc dev_t failed\n");
863                 return -1;
864         }
865
866         fm->cls = class_create(THIS_MODULE, FM_NAME);
867         if (IS_ERR(fm->cls)) {
868                 err = PTR_ERR(fm->cls);
869                 FM_ALERT("class_create err:%d\n", err);
870                 return err;            
871         }    
872         fm->dev = device_create(fm->cls, NULL, fm->dev_t, NULL, FM_NAME);
873
874         return 0;
875 }
876
877
878
879 static int fm_ops_ioctl(struct file *filp,
880                 unsigned int cmd, unsigned long arg)
881 {
882         int ret = 0;
883         struct fm *fm = container_of(filp->f_dentry->d_inode->i_cdev, struct fm, cdev);
884
885         FM_DEBUG("%s cmd(%x)\n", __func__, cmd);
886
887         switch(cmd)
888         {
889                 case FM_IOCTL_POWERUP:
890                         {
891                                 struct fm_tune_parm parm;
892                                 FM_DEBUG("FM_IOCTL_POWERUP\n");
893
894                                 // FIXME!!
895                                 //            if (!capable(CAP_SYS_ADMIN))
896                                 //                return -EPERM;
897
898                                 if (copy_from_user(&parm, (void*)arg, sizeof(struct fm_tune_parm)))
899                                         return -EFAULT;
900
901                                 if (down_interruptible(&fm_ops_mutex))
902                                         return -EFAULT;
903                                 ret = fm_powerup(fm, &parm);
904                                 up(&fm_ops_mutex);
905                                 if (copy_to_user((void*)arg, &parm, sizeof(struct fm_tune_parm)))
906                                         return -EFAULT;
907                                 //                      fm_low_power_wa(1);
908                                 break;
909                         }
910
911                 case FM_IOCTL_POWERDOWN:
912                         {
913                                 FM_DEBUG("FM_IOCTL_POWERDOWN\n");
914                                 // FIXME!!
915                                 //            if (!capable(CAP_SYS_ADMIN))
916                                 //                return -EPERM;
917                                 if (down_interruptible(&fm_ops_mutex))
918                                         return -EFAULT;
919                                 ret = fm_powerdown(fm);
920                                 up(&fm_ops_mutex);
921                                 //                      fm_low_power_wa(0);
922                                 break;
923                         }
924
925                         // tune (frequency, auto Hi/Lo ON/OFF )
926                 case FM_IOCTL_TUNE:
927                         {
928                                 struct fm_tune_parm parm;
929                                 FM_DEBUG("FM_IOCTL_TUNE\n");
930                                 // FIXME!
931                                 //            if (!capable(CAP_SYS_ADMIN))
932                                 //                return -EPERM;
933
934                                 if (copy_from_user(&parm, (void*)arg, sizeof(struct fm_tune_parm)))
935                                         return -EFAULT;
936
937                                 if (down_interruptible(&fm_ops_mutex))
938                                         return -EFAULT;
939                                 ret = fm_tune(fm, &parm);
940                                 up(&fm_ops_mutex);
941
942                                 if (copy_to_user((void*)arg, &parm, sizeof(struct fm_tune_parm)))
943                                         return -EFAULT;
944
945                                 break;
946                         }
947
948                 case FM_IOCTL_SEEK:
949                         {
950                                 struct fm_seek_parm parm;
951                                 FM_DEBUG("FM_IOCTL_SEEK\n");
952
953                                 // FIXME!!
954                                 //            if (!capable(CAP_SYS_ADMIN))
955                                 //              return -EPERM;
956
957                                 if (copy_from_user(&parm, (void*)arg, sizeof(struct fm_seek_parm)))
958                                         return -EFAULT;
959
960                                 if (down_interruptible(&fm_ops_mutex))
961                                         return -EFAULT;
962                                 ret = fm_seek(fm, &parm);
963                                 up(&fm_ops_mutex);
964
965                                 if (copy_to_user((void*)arg, &parm, sizeof(struct fm_seek_parm)))
966                                         return -EFAULT;
967
968                                 break;
969                         }
970
971                 case FM_IOCTL_SETVOL:
972                         {
973                                 uint32_t vol;
974                                 FM_DEBUG("FM_IOCTL_SETVOL\n");
975
976                                 // FIXME!!
977                                 //            if (!capable(CAP_SYS_ADMIN))
978                                 //              return -EPERM;
979
980                                 if(copy_from_user(&vol, (void*)arg, sizeof(uint32_t))) {
981                                         FM_ALERT("copy_from_user failed\n");
982                                         return -EFAULT;
983                                 }
984
985                                 if (down_interruptible(&fm_ops_mutex))
986                                         return -EFAULT;
987                                 ret = fm_setvol(fm, vol);
988                                 up(&fm_ops_mutex);
989
990                                 break;
991                         }
992
993                 case FM_IOCTL_GETVOL:
994                         {
995                                 uint32_t vol;
996                                 FM_DEBUG("FM_IOCTL_GETVOL\n");
997
998                                 // FIXME!!
999                                 //            if (!capable(CAP_SYS_ADMIN))
1000                                 //              return -EPERM;
1001
1002                                 if (down_interruptible(&fm_ops_mutex))
1003                                         return -EFAULT;
1004                                 ret = fm_getvol(fm, &vol);
1005                                 up(&fm_ops_mutex);
1006
1007                                 if (copy_to_user((void*)arg, &vol, sizeof(uint32_t)))
1008                                         return -EFAULT;
1009
1010                                 break;
1011                         }
1012
1013                 case FM_IOCTL_MUTE:
1014                         {
1015                                 uint32_t bmute;
1016                                 FM_DEBUG("FM_IOCTL_MUTE\n");
1017
1018                                 // FIXME!!
1019                                 //            if (!capable(CAP_SYS_ADMIN))
1020                                 //              return -EPERM;
1021                                 if (copy_from_user(&bmute, (void*)arg, sizeof(uint32_t)))
1022                                 {
1023                                         FM_DEBUG("copy_from_user mute failed!\n");
1024                                         return -EFAULT;    
1025                                 }
1026
1027                                 FM_DEBUG("FM_IOCTL_MUTE:%d\n", bmute); 
1028                                 if (down_interruptible(&fm_ops_mutex))
1029                                         return -EFAULT;
1030
1031                                 if (bmute){
1032                                         ret = RDAFM_enable_hmute(fm->i2c_client);
1033                                 }else{
1034                                         ret = RDAFM_clear_hmute(fm->i2c_client);
1035                                 }
1036
1037                                 up(&fm_ops_mutex);
1038
1039                                 break;
1040                         }
1041
1042                 case FM_IOCTL_GETRSSI:
1043                         {
1044                                 uint32_t rssi;
1045                                 FM_DEBUG("FM_IOCTL_GETRSSI\n");
1046
1047                                 // FIXME!!
1048                                 //            if (!capable(CAP_SYS_ADMIN))
1049                                 //              return -EPERM;
1050
1051                                 if (down_interruptible(&fm_ops_mutex))
1052                                         return -EFAULT;
1053
1054                                 ret = fm_getrssi(fm, &rssi);
1055                                 up(&fm_ops_mutex);
1056
1057                                 if (copy_to_user((void*)arg, &rssi, sizeof(uint32_t)))
1058                                         return -EFAULT;
1059
1060                                 break;
1061                         }
1062
1063                 case FM_IOCTL_RW_REG:
1064                         {
1065                                 struct fm_ctl_parm parm_ctl;
1066                                 FM_DEBUG("FM_IOCTL_RW_REG\n");
1067
1068                                 // FIXME!!
1069                                 //            if (!capable(CAP_SYS_ADMIN))
1070                                 //              return -EPERM;
1071
1072                                 if (copy_from_user(&parm_ctl, (void*)arg, sizeof(struct fm_ctl_parm)))
1073                                         return -EFAULT;
1074
1075                                 if (down_interruptible(&fm_ops_mutex))
1076                                         return -EFAULT;
1077
1078                                 if(parm_ctl.rw_flag == 0) //write
1079                                 {
1080                                         ret = RDAFM_write(fm->i2c_client, parm_ctl.addr, parm_ctl.val);
1081                                 }
1082                                 else
1083                                 {
1084                                         ret = RDAFM_read(fm->i2c_client, parm_ctl.addr, &parm_ctl.val);
1085                                 }
1086
1087                                 up(&fm_ops_mutex);
1088                                 if ((parm_ctl.rw_flag == 0x01) && (!ret)) // Read success.
1089                                 { 
1090                                         if (copy_to_user((void*)arg, &parm_ctl, sizeof(struct fm_ctl_parm)))
1091                                                 return -EFAULT;
1092                                 }
1093                                 break;
1094                         }
1095
1096                 case FM_IOCTL_GETCHIPID:
1097                         {
1098                                 uint16_t chipid;            
1099
1100                                 if (down_interruptible(&fm_ops_mutex))
1101                                         return -EFAULT;
1102
1103                                 RDAFM_GetChipID(fm->i2c_client, &chipid);
1104                                 //chipid = fm->chip_id;
1105                                 chipid = 0x6620;
1106                                 FM_DEBUG("FM_IOCTL_GETCHIPID:%04x\n", chipid);   
1107                                 up(&fm_ops_mutex);
1108
1109                                 if (copy_to_user((void*)arg, &chipid, sizeof(uint16_t)))
1110                                         return -EFAULT;
1111
1112                                 break;
1113                         }
1114
1115                 case FM_IOCTL_EM_TEST:
1116                         {
1117                                 struct fm_em_parm parm_em;
1118                                 FM_DEBUG("FM_IOCTL_EM_TEST\n");
1119
1120                                 // FIXME!!
1121                                 //            if (!capable(CAP_SYS_ADMIN))
1122                                 //              return -EPERM;
1123
1124                                 if (copy_from_user(&parm_em, (void*)arg, sizeof(struct fm_em_parm)))
1125                                         return -EFAULT;
1126
1127                                 if (down_interruptible(&fm_ops_mutex))
1128                                         return -EFAULT;
1129
1130                                 RDAFM_em_test(fm->i2c_client, parm_em.group_idx, parm_em.item_idx, parm_em.item_value);
1131
1132                                 up(&fm_ops_mutex);
1133
1134                                 break;
1135                         }
1136                 case FM_IOCTL_IS_FM_POWERED_UP:
1137                         {
1138                                 uint32_t powerup;
1139                                 FM_DEBUG("FM_IOCTL_IS_FM_POWERED_UP");
1140                                 if (fm->powerup) {
1141                                         powerup = 1;
1142                                 } else {
1143                                         powerup = 0;
1144                                 }
1145                                 if (copy_to_user((void*)arg, &powerup, sizeof(uint32_t)))
1146                                         return -EFAULT;
1147                                 break;
1148                         }
1149
1150 #ifdef FMDEBUG
1151                 case FM_IOCTL_DUMP_REG:
1152                         {
1153                                 uint16_t chipid = 0;
1154                                 if (down_interruptible(&fm_ops_mutex))
1155                                         return -EFAULT;
1156                                 RDAFM_GetChipID(fm->i2c_client, &chipid);
1157                                 up(&fm_ops_mutex);
1158
1159                                 break;
1160                         }
1161 #endif
1162
1163                 case FM_IOCTL_SCAN:
1164                         {
1165                                 struct fm_scan_parm parm;
1166                                 FM_DEBUG("FM_IOCTL_SCAN\n");
1167                                 if (false == fm->powerup){
1168                                         return -EFAULT;
1169                                 }
1170                                 if(copy_from_user(&parm, (void*)arg, sizeof(struct fm_scan_parm))){
1171                                         return -EFAULT;
1172                                 }
1173                                 if (down_interruptible(&fm_ops_mutex)){
1174                                         return -EFAULT;
1175                                 }
1176                                 fm_scan(fm, &parm);
1177                                 up(&fm_ops_mutex);
1178
1179                                 if(copy_to_user((void*)arg, &parm, sizeof(struct fm_scan_parm))){
1180                                         return -EFAULT;
1181                                 }
1182
1183                                 break;
1184                         }
1185
1186                 case FM_IOCTL_STOP_SCAN:
1187                         {
1188                                 FM_DEBUG("FM_IOCTL_STOP_SCAN\n");
1189                                 break;
1190                         }
1191
1192                 default:
1193                         {
1194                                 FM_DEBUG("default\n");
1195                                 break;
1196                         }
1197         }
1198
1199         return ret;
1200 }
1201 static loff_t fm_ops_lseek(struct file *filp, loff_t off, int whence)
1202 {
1203 //      struct fm *fm = filp->private_data;
1204
1205         if(whence == SEEK_END){
1206                 //fm_hwscan_stop(fm);
1207                 atomic_set(&scan_complete_flag, 0);
1208         }else if(whence == SEEK_SET){  
1209                 //FM_EVENT_SEND(fm->rds_event, FM_RDS_DATA_READY);
1210         }   
1211         return off;    
1212 }
1213
1214 static int fm_ops_open(struct inode *inode, struct file *filp)
1215 {
1216         struct fm *fm = container_of(inode->i_cdev, struct fm, cdev);
1217
1218         FM_DEBUG("%s\n", __func__);
1219
1220         if (down_interruptible(&fm_ops_mutex))
1221                 return -EFAULT;
1222
1223         // TODO: only have to set in the first time?
1224         // YES!!!!
1225
1226         fm->ref++;
1227
1228         up(&fm_ops_mutex);
1229
1230         filp->private_data = fm;
1231
1232         // TODO: check open flags
1233
1234         return 0;
1235 }
1236
1237 static int fm_ops_release(struct inode *inode, struct file *filp)
1238 {
1239         int err = 0;
1240         struct fm *fm = container_of(inode->i_cdev, struct fm, cdev);
1241
1242         FM_DEBUG("%s\n", __func__);
1243
1244         if (down_interruptible(&fm_ops_mutex))
1245                 return -EFAULT;
1246         fm->ref--;
1247         if(fm->ref < 1) {
1248                 if(fm->powerup == true) {
1249                         fm_powerdown(fm);           
1250                 }
1251         }
1252
1253         up(&fm_ops_mutex);
1254
1255         return err;
1256 }
1257
1258 static int fm_init(struct i2c_client *client)
1259 {
1260         int err;
1261         struct fm *fm = NULL;
1262         int ret = -1;
1263
1264
1265         FM_DEBUG("%s()\n", __func__);
1266         if (!(fm = kzalloc(sizeof(struct fm), GFP_KERNEL)))
1267         {
1268                 FM_ALERT("-ENOMEM\n");
1269                 err = -ENOMEM;
1270                 goto ERR_EXIT;
1271         }
1272
1273         fm->ref = 0;
1274         fm->powerup = false;
1275         atomic_set(&scan_complete_flag, 0);
1276
1277         // First, read 5802NM chip ID
1278         FM_DEBUG("%s()First, read 5802NM chip ID\n", __func__);
1279         ret = RDAFM_GetChipID(client, &RDAFM_CHIP_ID);
1280         FM_DEBUG("%s() 5802NM chip ID = 0x%04x\n", __func__, RDAFM_CHIP_ID);
1281         // if failed, means use FM in 5990P_E
1282         if(ret < 0){
1283                 // enable the FM chip in combo
1284                 FM_DEBUG("%s() enable the FM chip in combo\n", __func__);
1285                 ret = rda_fm_power_on();
1286                 if(ret < 0){
1287                         err = -ENOMEM;
1288                         goto ERR_EXIT;
1289                 }
1290                 msleep(100);
1291                 ret = RDAFM_GetChipID(client, &RDAFM_CHIP_ID);
1292                 FM_DEBUG("%s() the FM in combo chip ID = 0x%04x\n", __func__, RDAFM_CHIP_ID);
1293                 if(ret < 0){
1294                         err = -ENOMEM;
1295                         goto ERR_EXIT;
1296                 }else{
1297                         fm->chip_id = RDAFM_CHIP_ID;
1298                 }
1299
1300                 // disable the FM chip for power saving
1301                 ret = rda_fm_power_off();
1302                 if(ret < 0){
1303                         err = -ENOMEM;
1304                         goto ERR_EXIT;
1305                 }
1306         }else{
1307                 fm->chip_id = RDAFM_CHIP_ID;
1308         }
1309
1310
1311
1312         if ((err = fm_setup_cdev(fm)))
1313         {
1314                 goto ERR_EXIT;
1315         }
1316
1317         g_fm_struct = fm;
1318         fm->i2c_client = client;
1319         i2c_set_clientdata(client, fm);
1320
1321
1322         /***********Add porc file system*************/
1323
1324         g_fm_proc = create_proc_entry(FM_PROC_FILE, 0444, NULL);
1325         if (g_fm_proc == NULL) {
1326                 FM_ALERT("create_proc_entry failed\n");
1327                 err = -ENOMEM;
1328                 goto ERR_EXIT;
1329         } else {
1330                 g_fm_proc->read_proc = fm_proc_read;
1331                 g_fm_proc->write_proc = NULL;
1332                 //g_fm_proc->owner = THIS_MODULE;
1333                 FM_ALERT("create_proc_entry success\n");
1334         }
1335
1336         /********************************************/
1337
1338         FM_DEBUG("fm_init is ok!\n");
1339
1340         return 0;
1341
1342 ERR_EXIT:
1343         kfree(fm);
1344
1345         return err;
1346 }
1347
1348 static int fm_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data)
1349 {
1350         int cnt= 0;
1351         struct fm *fm  = g_fm_struct;
1352         FM_ALERT("Enter fm_proc_read.\n");
1353         if(off != 0)
1354                 return 0;
1355         if (fm != NULL && fm->powerup) {
1356                 cnt = sprintf(page, "1\n");
1357         } else {
1358                 cnt = sprintf(page, "0\n");
1359         }
1360         *eof = 1;
1361         FM_ALERT("Leave fm_proc_read. cnt = %d\n", cnt);
1362         return cnt;
1363 }
1364
1365
1366 static int fm_destroy(struct fm *fm)
1367 {
1368         int err = 0;
1369
1370         FM_DEBUG("%s\n", __func__);
1371
1372         device_destroy(fm->cls, fm->dev_t);
1373         class_destroy(fm->cls);
1374
1375         cdev_del(&fm->cdev);
1376         unregister_chrdev_region(fm->dev_t, 1);
1377
1378         fm_powerdown(fm);
1379
1380         /***********************************/
1381         remove_proc_entry(FM_PROC_FILE, NULL);
1382
1383         /**********************************/
1384
1385         // FIXME: any other hardware configuration ?
1386
1387         // free all memory
1388         kfree(fm);
1389
1390         return err;
1391 }
1392
1393 /*
1394  *  fm_powerup
1395  */
1396 static int fm_powerup(struct fm *fm, struct fm_tune_parm *parm)
1397 {
1398         int i;
1399         uint16_t tRegValue = 0x0002;
1400         int ret = -1;
1401
1402         struct i2c_client *client = fm->i2c_client;
1403
1404         if (fm->powerup)
1405         {
1406                 parm->err = FM_BADSTATUS;
1407                 return -EPERM;
1408         }
1409
1410         // if chip_id is ID_RDA5820NS, enable the FM chip in combo
1411         if(fm->chip_id == ID_RDA5820NS){
1412                 ret = rda_fm_power_on();
1413                 if(ret < 0){
1414                         return -EPERM;
1415                 }
1416                 msleep(100);
1417         }
1418
1419
1420         //Reset RDA FM
1421         tRegValue = 0x0002;
1422         RDAFM_write(client, 0x02, tRegValue);
1423         msleep(100);
1424
1425
1426         if (ID_RDA5802N == RDAFM_CHIP_ID){
1427                 for (i=0; i<((sizeof(RDA5802N_initialization_reg)) / (sizeof(uint16_t))); i++)
1428                 {
1429                         ret = RDAFM_write(client, i+2, RDA5802N_initialization_reg[i]);
1430
1431                         if (ret < 0)
1432                         {
1433                                 FM_DEBUG("fm_powerup init failed!\n");
1434
1435                                 parm->err = FM_FAILED;
1436
1437                                 return -EPERM;
1438                         }
1439                 }
1440
1441         }else if (ID_RDA5820NS == RDAFM_CHIP_ID){
1442                 if(RDA_RADIO_WorkType == FM_RECEIVER){
1443                         for (i = 0; i < ((sizeof(RDA5820NS_RX_initialization_reg)) / (sizeof(RDA_FM_REG_T))); i++)
1444                         {
1445                                 if(RDA5820NS_RX_initialization_reg[i].address == 0xFF){
1446                                         msleep(RDA5820NS_RX_initialization_reg[i].value);
1447                                 }else{
1448                                         ret = RDAFM_write(client, RDA5820NS_RX_initialization_reg[i].address, RDA5820NS_RX_initialization_reg[i].value);
1449                                         if (ret < 0)
1450                                         {
1451                                                 FM_DEBUG("fm_powerup init failed!\n");
1452                                                 parm->err = FM_FAILED;
1453                                                 return -EPERM;
1454                                         }
1455                                 }
1456                         }
1457                 }else{
1458                         for (i = 0; i < ((sizeof(RDA5820NS_TX_initialization_reg)) / (sizeof(RDA_FM_REG_T))); i++)
1459                         {
1460                                 if(RDA5820NS_TX_initialization_reg[i].address == 0xFF){
1461                                         msleep(RDA5820NS_TX_initialization_reg[i].value);
1462                                 }else{
1463                                         ret = RDAFM_write(client, RDA5820NS_TX_initialization_reg[i].address, RDA5820NS_TX_initialization_reg[i].value);
1464                                         if (ret < 0)
1465                                         {
1466                                                 FM_DEBUG("fm_powerup init failed!\n");
1467                                                 parm->err = FM_FAILED;
1468                                                 return -EPERM;
1469                                         }
1470                                 }
1471                         }
1472                 }
1473
1474         }
1475
1476
1477         FM_DEBUG("pwron ok\n");
1478         fm->powerup = true;
1479
1480         if (fm_tune(fm, parm) < 0)
1481         {
1482                 return -EPERM;
1483         }
1484         fm_tune_data.byPowerUp = true;
1485         memcpy(&fm_tune_data.parm, parm, sizeof(fm_tune_data.parm));
1486
1487         parm->err = FM_SUCCESS;
1488
1489         return 0;
1490
1491 }
1492
1493 /*
1494  *  fm_powerdown
1495  */
1496 static int fm_powerdown(struct fm *fm)
1497 {
1498         uint16_t tRegValue = 0;
1499         int ret = -1;
1500         struct i2c_client *client = fm->i2c_client;
1501
1502         RDAFM_read(client, 0x02, &tRegValue);
1503         tRegValue &= (~(1 << 0));
1504         RDAFM_write(client, 0x02, tRegValue);
1505
1506         if(fm->chip_id == ID_RDA5820NS){
1507                 ret = rda_fm_power_off();
1508                 if(ret < 0){
1509                         return -EPERM;
1510                 }
1511         }
1512
1513         fm->powerup = false;
1514         FM_ALERT("pwrdown ok\n");
1515
1516         return 0;
1517 }
1518
1519 /*
1520  *  fm_seek
1521  */
1522 static int fm_seek(struct fm *fm, struct fm_seek_parm *parm)
1523 {
1524         int ret = 0;
1525         uint16_t val = 0;
1526         uint8_t spaec = 1;
1527         uint16_t tFreq = 875;
1528         uint16_t tRegValue = 0;
1529         uint16_t bottomOfBand = 875;
1530         int falseStation = -1;
1531
1532
1533         struct i2c_client *client = fm->i2c_client;
1534
1535         if (!fm->powerup)
1536         {
1537                 parm->err = FM_BADSTATUS;
1538                 return -EPERM;
1539         }
1540
1541         if (parm->space == FM_SPACE_100K)
1542         {
1543                 spaec = 1;
1544                 val &= (~((1<<0) | (1<<1)));
1545         }
1546         else if (parm->space == FM_SPACE_200K)
1547         {
1548                 spaec = 2;
1549                 val &= (~(1<<1));
1550                 val |= (1<<0);
1551         }
1552         else
1553         {
1554                 parm->err = FM_EPARM;
1555                 return -EPERM;
1556         }
1557
1558         if (parm->band == FM_BAND_UE)
1559         {
1560                 val &= (~((1<<2) | (1<<3)));
1561                 bottomOfBand = 875;
1562                 fm->min_freq = 875;
1563                 fm->max_freq = 1080;
1564         }
1565         else if (parm->band == FM_BAND_JAPAN) 
1566         {
1567                 val &= (~(1<<3));
1568                 val |= (1 << 2);
1569                 bottomOfBand = 760;
1570                 fm->min_freq = 760;
1571                 fm->max_freq = 910;
1572         }
1573         else if (parm->band == FM_BAND_JAPANW) {
1574                 val &= (~(1<<2));
1575                 val |= (1 << 3);
1576                 bottomOfBand = 760;
1577                 fm->min_freq = 760;
1578                 fm->max_freq = 1080;
1579         }
1580         else
1581         {
1582                 FM_ALERT("band:%d out of range\n", parm->band);
1583                 parm->err = FM_EPARM;
1584                 return -EPERM;
1585         }
1586
1587         if (parm->freq < fm->min_freq || parm->freq > fm->max_freq) {
1588                 FM_ALERT("freq:%d out of range\n", parm->freq);
1589                 parm->err = FM_EPARM;
1590                 return -EPERM;
1591         }
1592
1593         if (parm->seekth > 0x0B) {
1594                 FM_ALERT("seekth:%d out of range\n", parm->seekth);
1595                 parm->err = FM_EPARM;
1596                 return -EPERM;
1597         }
1598
1599         RDAFM_read(client, 0x05, &tRegValue);
1600         tRegValue &= (~(0x7f<<8));
1601         //tRegValue |= ((parm->seekth & 0x7f) << 8);
1602         tRegValue |= ((0x8 & 0x7f) << 8);
1603         RDAFM_write(client, 0x05, tRegValue);
1604
1605
1606 #ifdef FMDEBUG
1607         if (parm->seekdir == FM_SEEK_UP)
1608                 FM_DEBUG("seek %d up\n", parm->freq);
1609         else
1610                 FM_DEBUG("seek %d down\n", parm->freq);
1611 #endif
1612
1613         // (1) set hmute bit
1614         RDAFM_enable_hmute(client);
1615
1616         tFreq = parm->freq;
1617
1618         do {
1619                 if (parm->seekdir == FM_SEEK_UP)
1620                         tFreq += spaec;
1621                 else
1622                         tFreq -= spaec;
1623
1624                 if (tFreq > fm->max_freq)
1625                         tFreq = fm->min_freq;
1626                 if (tFreq < fm->min_freq)
1627                         tFreq = fm->max_freq;
1628
1629                 val = (((tFreq - bottomOfBand+5) << 6) | (1 << 4) | (val & 0x0f));
1630                 RDAFM_write(client, 0x03, val);
1631                 msleep(40);
1632                 ret = RDAFM_read(client, 0x0B, &tRegValue);
1633                 if (ret < 0)
1634                 {
1635                         FM_DEBUG("fm_seek: read register failed tunning freq = %4X\n", tFreq);
1636                         falseStation = -1;
1637                 }
1638                 else
1639                 {
1640                         if ((tRegValue & 0x0100) == 0x0100)
1641                                 falseStation = 0;
1642                         else
1643                                 falseStation = -1;
1644                 }
1645
1646                 if(falseStation == 0)
1647                         break;
1648         }while(tFreq != parm->freq);
1649
1650
1651         //clear hmute
1652         RDAFM_clear_hmute(client);
1653
1654         if (falseStation == 0) // seek successfully
1655         {    
1656                 parm->freq = tFreq;
1657                 FM_ALERT("fm_seek success, freq:%d\n", parm->freq);
1658                 parm->err = FM_SUCCESS;
1659
1660
1661         }
1662         else
1663         {
1664                 FM_ALERT("fm_seek failed, invalid freq\n");
1665                 parm->err = FM_SEEK_FAILED;
1666                 ret = -1;
1667         }
1668
1669         return ret;
1670 }
1671
1672 /*
1673  *  fm_scan
1674  */
1675 static int  fm_scan(struct fm *fm, struct fm_scan_parm *parm)
1676 {
1677         int ret = 0;
1678         uint16_t tRegValue = 0;
1679         uint16_t scandir = RDA599X_FM_SCAN_UP; //scandir ËÑË÷·½Ïò
1680         uint8_t space = 1; 
1681         struct i2c_client *client = fm->i2c_client;
1682
1683         if (!fm->powerup){
1684                 parm->err = FM_BADSTATUS;
1685                 return -EPERM;
1686         }
1687
1688         RDAFM_read(client, 0x03, &tRegValue);
1689
1690         if (parm->space == FM_SPACE_100K){
1691                 space = 1;
1692                 tRegValue &= (~((1<<0) | (1<<1))); //set 03H's bit[1:0] to 00
1693         }else if (parm->space == FM_SPACE_200K) {
1694                 space = 2;
1695                 tRegValue &= (~(1<<1)); //clear bit[1]
1696                 tRegValue |= (1<<0);    //set bit[0]
1697         }else{
1698                 //default
1699                 space = 1;
1700                 tRegValue &= (~((1<<0) | (1<<1))); //set 03H's bit[1:0] to 00
1701         }
1702
1703         if(parm->band == FM_BAND_UE){
1704                 tRegValue &= (~((1<<2) | (1<<3)));
1705                 fm->min_freq = 875;
1706                 fm->max_freq = 1080;
1707         }else if(parm->band == FM_BAND_JAPAN){
1708                 tRegValue &= (~(1<<3));
1709                 tRegValue |= (1 << 2);
1710                 fm->min_freq = 760;
1711                 fm->max_freq = 900;
1712         }else if(parm->band == FM_BAND_JAPANW){
1713                 tRegValue &= (~(1<<2));
1714                 tRegValue |= (1 << 3);
1715                 fm->min_freq = 760;
1716                 fm->max_freq = 1080;
1717         }else{
1718                 parm->err = FM_EPARM;
1719                 return -EPERM;
1720         }
1721
1722         //set space and band
1723         RDAFM_write(client, 0x03, tRegValue);
1724         msleep(40);
1725
1726
1727         if(RDAFM_Scan(client, fm->min_freq, fm->max_freq, &(parm->freq), parm->ScanTBL, &(parm->ScanTBLSize), scandir, space)){
1728                 parm->err = FM_SUCCESS;
1729         }else{
1730                 parm->err = FM_SEEK_FAILED;
1731         }
1732
1733         return ret;
1734 }
1735
1736
1737 static int fm_setvol(struct fm *fm, uint32_t vol)
1738 {
1739         int ret = 0;
1740         uint16_t tRegValue = 0;
1741         struct i2c_client *client = fm->i2c_client;
1742
1743         if (vol > 15)
1744                 vol = 15;
1745
1746         FM_DEBUG("fm_setvol:%d\n", vol);
1747
1748         ret = RDAFM_read(client, 0x05, &tRegValue);
1749         if (ret)
1750                 return -EPERM;
1751         tRegValue &= ~(0x000f);
1752         tRegValue |= vol;
1753
1754         ret = RDAFM_write(client, 0x05, tRegValue);
1755         if (ret)
1756                 return -EPERM;
1757
1758         return 0;
1759 }
1760
1761 static int fm_getvol(struct fm *fm, uint32_t *vol)
1762 {
1763         int ret = 0;
1764         uint16_t tRegValue;
1765         struct i2c_client *client = fm->i2c_client;
1766
1767         ret = RDAFM_read(client, 0x05, &tRegValue);
1768         if (ret)
1769                 return -EPERM;
1770
1771         if (ret)
1772                 return -EPERM;
1773
1774         *vol = (tRegValue & 0x000F);
1775
1776         return 0;
1777 }
1778
1779 static int fm_getrssi(struct fm *fm, uint32_t *rssi)
1780 {
1781         int ret = 0;
1782         uint16_t tRegValue;
1783         struct i2c_client *client = fm->i2c_client;
1784
1785         ret = RDAFM_read(client, 0x0B, &tRegValue);
1786         if (ret)
1787                 return -EPERM;
1788
1789
1790         *rssi = (uint32_t)((tRegValue >> 9) & RDAFM_MASK_RSSI);
1791
1792         FM_DEBUG("rssi value:%d\n", *rssi);
1793
1794         return 0;
1795 }
1796
1797 /*
1798  *  fm_tune
1799  */
1800 static int fm_tune(struct fm *fm, struct fm_tune_parm *parm)
1801 {
1802         int ret;
1803         uint16_t val = 0;
1804         uint8_t space = 1;
1805         uint16_t bottomOfBand = 875;
1806
1807         struct i2c_client *client = fm->i2c_client;
1808
1809         FM_DEBUG("%s\n", __func__);
1810
1811         if (!fm->powerup)
1812         {
1813                 parm->err = FM_BADSTATUS;
1814                 return -EPERM;
1815         } 
1816
1817         if (parm->space == FM_SPACE_100K)
1818         {
1819                 space = 1;
1820                 val &= (~((1<<0) | (1<<1)));
1821         }
1822         else if (parm->space == FM_SPACE_200K)
1823         {
1824                 space = 2;
1825                 val |= (1<<0);
1826                 val &= (~(1<<1));
1827         }
1828         else
1829         {
1830                 parm->err = FM_EPARM;
1831                 return -EPERM;
1832         }
1833
1834         if (parm->band == FM_BAND_UE)
1835         {
1836                 val &= (~((1<<2) | (1<<3)));
1837                 bottomOfBand = 875;
1838                 fm->min_freq = 875;
1839                 fm->max_freq = 1080;
1840         }
1841         else if (parm->band == FM_BAND_JAPAN) 
1842         {
1843                 val &= (~(1<<3));
1844                 val |= (1 << 2);
1845                 bottomOfBand = 760;
1846                 fm->min_freq = 760;
1847                 fm->max_freq = 910;
1848         }
1849         else if (parm->band == FM_BAND_JAPANW) {
1850                 val &= (~(1<<2));
1851                 val |= (1 << 3);
1852                 bottomOfBand = 760;
1853                 fm->min_freq = 760;
1854                 fm->max_freq = 1080;
1855         }
1856         else
1857         {
1858                 FM_ALERT("band:%d out of range\n", parm->band);
1859                 parm->err = FM_EPARM;
1860                 return -EPERM;
1861         }
1862
1863         if (parm->freq < fm->min_freq || parm->freq > fm->max_freq) {
1864                 FM_ALERT("freq:%d out of range\n", parm->freq);
1865                 parm->err = FM_EPARM;
1866                 return -EPERM;
1867         }
1868
1869         FM_DEBUG("fm_tune, freq:%d\n", parm->freq);
1870
1871         //RDAFM_enable_hmute(client);
1872
1873         val = (((parm->freq - bottomOfBand + 5) << 6) | (1 << 4) | (val & 0x0f));
1874
1875         ret = RDAFM_write(client, 0x03, val);
1876         if (ret < 0)
1877         {
1878                 FM_ALERT("fm_tune write freq failed\n");
1879                 parm->err = FM_SEEK_FAILED;
1880                 return ret;
1881         }
1882         msleep(40);
1883
1884         return ret;
1885 }
1886
1887 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31))
1888 /*
1889  *  fm_i2c_attach_adapter
1890  */
1891 static int fm_i2c_attach_adapter(struct i2c_adapter *adapter)
1892 {
1893         int err = 0;
1894
1895         if (adapter->id == RDAFM_I2C_PORT)
1896         {
1897                 return i2c_probe(adapter, &RDAFM_addr_data, fm_i2c_detect);
1898         }
1899
1900         return err;
1901 }
1902
1903 /*
1904  *  fm_i2c_detect
1905  *  This function is called by i2c_detect
1906  */
1907 static int fm_i2c_detect(struct i2c_adapter *adapter, int addr, int kind)
1908 {
1909         int err;
1910         struct i2c_client *client = NULL;
1911
1912         /* skip this since MT6516 shall support all the needed functionalities
1913            if (!i2c_check_functionality(adapter, xxx))
1914            {
1915            FM_DEBUG("i2c_check_functionality failed\n");
1916            return -ENOTSUPP;
1917            }
1918            */
1919
1920         /* initial i2c client */
1921         if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL)))
1922         {
1923                 FM_ALERT("kzalloc failed\n");
1924                 err = -ENOMEM;
1925                 goto ERR_EXIT;
1926         }
1927
1928         client->addr = addr;
1929         client->adapter = adapter;
1930         client->driver = &RDAFM_driver;
1931         client->flags = 0;
1932         strncpy(client->name, "RDA FM RADIO", I2C_NAME_SIZE);
1933
1934         if ((err = fm_init(client)))
1935         {
1936                 FM_ALERT("fm_init ERR:%d\n", err);
1937                 goto ERR_EXIT;
1938         }
1939
1940         if (err = i2c_attach_client(client))
1941         {
1942                 FM_ALERT("i2c_attach_client ERR:%d\n", err);
1943                 goto ERR_EXIT;
1944         }
1945
1946         return 0;
1947
1948 ERR_EXIT:
1949         kfree(client);
1950
1951         return err;
1952 }
1953 static int fm_i2c_detach_client(struct i2c_client *client)
1954 {
1955         int err = 0;
1956         struct fm *fm = i2c_get_clientdata(client);
1957
1958         FM_DEBUG("fm_i2c_detach_client\n");
1959
1960         err = i2c_detach_client(client);
1961         if (err)
1962         {
1963                 dev_err(&client->dev, "fm_i2c_detach_client failed\n");
1964                 return err;
1965         }
1966
1967         fm_destroy(fm);
1968         kfree(client);
1969
1970         return err;
1971 }
1972 #else
1973 static int fm_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
1974 {
1975         int err = -1;
1976         FM_DEBUG("fm_i2c_probe\n");    
1977         //client->timing = 50;
1978         //client->timing = 200;
1979         if ((err = fm_init(client)))
1980         {
1981                 FM_ALERT("fm_init ERR:%d\n", err);
1982                 goto ERR_EXIT;
1983         }   
1984
1985         return 0;   
1986
1987 ERR_EXIT:
1988         return err;    
1989 }
1990
1991 static int fm_i2c_detect(struct i2c_client *client, int kind, struct i2c_board_info *info)
1992 {
1993         FM_DEBUG("fm_i2c_detect\n");
1994         strcpy(info->type, RDAFM_DEV);
1995         return 0;
1996 }
1997
1998 static int fm_i2c_remove(struct i2c_client *client)
1999 {
2000         int err = 0;
2001         struct fm *fm = i2c_get_clientdata(client);
2002
2003         FM_DEBUG("fm_i2c_remove\n");
2004         if(fm)
2005         {    
2006                 fm_destroy(fm);
2007                 fm = NULL;
2008         }
2009
2010         return err;
2011 }
2012 #endif
2013
2014 int  i2c_static_add_device(struct i2c_board_info *info)
2015 {
2016         struct i2c_adapter *adapter;
2017         struct i2c_client  *client;
2018         int    ret; 
2019
2020         adapter = i2c_get_adapter(RDAFM_I2C_PORT);
2021         if (!adapter) {
2022                 FM_DEBUG("%s: can't get i2c adapter\n", __func__);
2023                 ret = -ENODEV;
2024                 goto i2c_err;
2025         }    
2026
2027         client = i2c_new_device(adapter, info);
2028         if (!client) {
2029                 FM_DEBUG("%s:  can't add i2c device at 0x%x\n",
2030                                 __FUNCTION__, (unsigned int)info->addr);
2031                 ret = -ENODEV;
2032                 goto i2c_err;
2033         }    
2034
2035         i2c_put_adapter(adapter);
2036
2037         return 0;
2038
2039 i2c_err:
2040         return ret;
2041 }
2042
2043 static int mt_fm_probe(struct platform_device *pdev)
2044 {
2045         int err = -1;
2046         FM_ALERT("mt_fm_probe\n");
2047         err = i2c_static_add_device(&i2c_rdafm);
2048         if (err < 0){
2049                 FM_DEBUG("%s(): add i2c device error, err = %d\n", __func__, err);
2050                 return err;
2051         }
2052
2053         // Open I2C driver
2054         err = i2c_add_driver(&RDAFM_driver);
2055         if (err)
2056         {
2057                 FM_ALERT("i2c err\n");
2058         }
2059
2060         return err;   
2061
2062
2063 static int mt_fm_remove(struct platform_device *pdev)
2064 {
2065         FM_ALERT("mt_fm_remove\n");
2066         i2c_unregister_device(g_fm_struct->i2c_client);
2067         i2c_del_driver(&RDAFM_driver); 
2068
2069         return 0; 
2070 }
2071
2072
2073 static struct platform_driver mt_fm_dev_drv =
2074 {
2075         .probe   = mt_fm_probe,
2076         .remove  = mt_fm_remove,
2077 #if 0//def CONFIG_PM //Not need now   
2078         .suspend = mt_fm_suspend,
2079         .resume  = mt_fm_resume,
2080 #endif    
2081         .driver = {
2082                 .name   = FM_NAME,
2083                 .owner  = THIS_MODULE,    
2084         }
2085 };
2086
2087 #if defined(MTK_MT6515)
2088 static struct platform_device mt_fm_device = {
2089         .name   = FM_NAME,
2090         .id = -1, 
2091 };
2092 #endif
2093
2094
2095 /*
2096  *  mt_fm_init
2097  */
2098 static int __init mt_fm_init(void)
2099 {
2100         int err = 0;
2101
2102         FM_DEBUG("mt_fm_init\n");
2103 #if defined(MTK_MT6515)
2104         err = platform_device_register(&mt_fm_device);
2105         if(err){
2106                 FM_DEBUG("platform_device_register  fail\n");
2107                 return err;
2108         }else{
2109                 FM_DEBUG("platform_device_register  success\n");
2110         }
2111 #endif
2112         err = platform_driver_register(&mt_fm_dev_drv);
2113         if (err)
2114         {
2115                 FM_DEBUG("platform_driver_register failed\n");
2116         }else{
2117                 FM_DEBUG("platform_driver_register success\n");
2118         }
2119
2120         return err;
2121 }
2122
2123 /*
2124  *  mt_fm_exit
2125  */
2126 static void __exit mt_fm_exit(void)
2127 {
2128         FM_DEBUG("mt_fm_exit\n");
2129         platform_driver_unregister(&mt_fm_dev_drv);
2130 #if defined(MTK_MT6515)
2131         platform_device_unregister(&mt_fm_device);
2132 #endif
2133 }
2134
2135 module_init(mt_fm_init);
2136 module_exit(mt_fm_exit);
2137
2138 MODULE_LICENSE("GPL");
2139 MODULE_DESCRIPTION("MediaTek FM Driver");
2140 MODULE_AUTHOR("William Chung <William.Chung@MediaTek.com>");
2141
2142