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>
8 #include <linux/cdev.h>
10 #include <linux/version.h> /* constant of kernel version */
11 #include <asm/uaccess.h> // get_user()
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>
20 // if need debug, define FMDEBUG
23 // if your platform is MT6515/6575, define MTK_MT6515
26 // if your platform is MT6515/6575 and MTK FM is MT6626, define MT6626
29 #define FM_ALERT(f, s...) \
31 printk(KERN_ALERT "RDAFM " f, ## s); \
35 #define FM_DEBUG(f, s...) \
37 printk("RDAFM " f, ## s); \
40 #define FM_DEBUG(f, s...)
43 #define RDA599X_SCANTBL_SIZE 16 //16*uinit16_t
44 #define RDA599X_FM_SCAN_UP 0x0
45 #define RDA599X_FM_SCAN_DOWN 0x01
47 extern int rda_gpio_i2c_read_1_addr_2_data(uint8_t chipAddr, uint8_t regAddr, unsigned short *buffer);
48 extern int rda_gpio_i2c_write_1_addr_2_data(uint8_t chipAddr, uint8_t regAddr, unsigned short data);
49 extern int rda_fm_power_off(void);
50 extern int rda_fm_power_on(void);
52 /******************************************************************************
53 * CONSTANT DEFINITIONS
54 *****************************************************************************/
55 #define RDAFM_SLAVE_ADDR 0x11 //RDA FM Chip address
57 #define RDAFM_MASK_RSSI 0X7F // RSSI
61 #define ID_RDA5802E 0x5804
62 #define ID_RDA5802H 0x5801
63 #define ID_RDA5802N 0x5808
64 #define ID_RDA5820 0x5805
65 #define ID_RDA5820NS 0x5820
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;
73 #define FM_PROC_FILE "fm"
75 /******************************************************************************
76 * STRUCTURE DEFINITIONS
77 *****************************************************************************/
79 enum RDAFM_CHIP_TYPE {
80 CHIP_TYPE_RDA5802E = 0,
97 struct fm_tune_parm parm
99 static FM_TUNE_T fm_tune_data = {false, {}};
103 FM_RECEIVER, //5800,5802,5804
104 FM_TRANSMITTER, //5820
111 }RDA_FM_POWER_STATE_T;
117 // uint16_t device_id;
120 uint16_t min_freq; // KHz
121 uint16_t max_freq; // KHz
122 uint8_t band; // TODO
126 // struct i2c_client *i2c_client;
132 /******************************************************************************
133 * FUNCTION PROTOTYPES
134 *****************************************************************************/
137 static int RDAFM_clear_hmute(uint8_t chipAddr);
138 static int RDAFM_enable_hmute(uint8_t chipAddr);
139 static bool RDAFM_Scan(uint8_t chipAddr,
140 uint16_t min_freq, uint16_t max_freq,
141 uint16_t *pFreq, //get the valid freq after scan
143 uint16_t *ScanTBLsize,
148 static int RDAFM_read(uint8_t chipAddr, uint8_t addr, uint16_t *val);
149 static int RDAFM_write(uint8_t chipAddr, uint8_t addr, uint16_t val);
150 static int fm_setup_cdev(struct fm *fm);
151 static int fm_ops_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
152 static loff_t fm_ops_lseek(struct file *filp, loff_t off, int whence);
153 static int fm_ops_open(struct inode *inode, struct file *filp);
154 static int fm_ops_release(struct inode *inode, struct file *filp);
156 static int fm_init(void);
157 static int fm_destroy(struct fm *fm);
158 static int fm_powerup(struct fm *fm, struct fm_tune_parm *parm);
159 static int fm_powerdown(struct fm *fm);
161 static int fm_tune(struct fm *fm, struct fm_tune_parm *parm);
162 static int fm_seek(struct fm *fm, struct fm_seek_parm *parm);
163 static int fm_scan(struct fm *fm, struct fm_scan_parm *parm);
164 static int fm_setvol(struct fm *fm, uint32_t vol);
165 static int fm_getvol(struct fm *fm, uint32_t *vol);
166 static int fm_getrssi(struct fm *fm, uint32_t *rssi);
167 static int fm_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data);
174 static uint16_t RDAFM_CHIP_ID = 0x5808;
175 static RDA_RADIO_WORK_E RDA_RADIO_WorkType = FM_RECEIVER;
180 static uint16_t RDA5802N_initialization_reg[]={
184 0xC6ED, //0x86AD, //05h
206 0xF7CF, //Ìá¸ßÔ¶¶ËÔëÉùÒÖÖÆ
207 0x2414, //0x2ADC, //0x1ch ÌáÉýVIO VDDÖ®¼äѹ²îÒýÆðµÄ²»Á¼
219 static uint16_t RDA5802N_initialization_reg[]={
259 static RDA_FM_REG_T RDA5820NS_TX_initialization_reg[]={
261 {0xFF, 100}, // if address is 0xFF, sleep value ms
272 static RDA_FM_REG_T RDA5820NS_RX_initialization_reg[]={
273 {0x02, 0x0002}, //Soft reset
275 {0x02, 0xC001}, //Power Up
276 {0x05, 0x888F}, //LNAP 0x884F --LNAN
287 static struct file_operations fm_ops = {
288 .owner = THIS_MODULE,
289 .unlocked_ioctl = fm_ops_ioctl,
290 .llseek = fm_ops_lseek,
292 .release = fm_ops_release,
295 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
296 static DECLARE_MUTEX(fm_ops_mutex);
298 DEFINE_SEMAPHORE(fm_ops_mutex);
301 static int RDAFM_GetChipID(uint8_t chipAddr, uint16_t *pChipID)
305 uint16_t val = 0x0002;
308 err = RDAFM_write(chipAddr, 0x02, val);
311 FM_DEBUG("RDAFM_GetChipID: reset FM chip failed!\n");
319 err = RDAFM_read(chipAddr, 0x0C, &val);
322 if ((0x5802 == val) || (0x5803 == val))
324 err = RDAFM_read(chipAddr, 0x0E, &val);
332 FM_DEBUG("RDAFM_GetChipID: Chip ID = %04X\n", val);
338 else if ((0x5805 == val) || (0x5820 == val))
346 FM_DEBUG("RDAFM_GetChipID: get chip ID failed! get value = %04X\n", val);
355 FM_DEBUG("RDAFM_GetChipID: get chip ID failed!\n");
367 static int RDAFM_read(uint8_t chipAddr, uint8_t regAddr, uint16_t *val)
370 ret = rda_gpio_i2c_read_1_addr_2_data(chipAddr, regAddr, val);
378 static int RDAFM_write(uint8_t chipAddr, uint8_t regAddr, uint16_t val)
382 n = rda_gpio_i2c_write_1_addr_2_data(chipAddr, regAddr, val);
385 FM_ALERT("RDAFM_write send:0x%X err:%d\n", regAddr, n);
393 static int RDAFM_clear_hmute(uint8_t chipAddr)
396 uint16_t tRegValue = 0;
398 FM_DEBUG("RDAFM_clear_hmute\n");
400 ret = RDAFM_read(chipAddr, 0x02, &tRegValue);
403 FM_ALERT("RDAFM_clear_hmute read register failed!\n");
407 tRegValue |= (1 << 14);
409 ret = RDAFM_write(chipAddr, 0x02, tRegValue);
413 FM_ALERT("RDAFM_clear_hmute write register failed!\n");
417 if(fm_tune_data.byPowerUp){
418 if (fm_tune(g_fm_struct, &(fm_tune_data.parm)) < 0)
420 fm_tune_data.byPowerUp = false;
421 memset(&fm_tune_data.parm, 0, sizeof(fm_tune_data.parm));
424 fm_tune_data.byPowerUp = false;
425 memset(&fm_tune_data.parm, 0, sizeof(fm_tune_data.parm));
433 static int RDAFM_enable_hmute(uint8_t chipAddr)
436 uint16_t tRegValue = 0;
438 FM_DEBUG("RDAFM_enable_hmute\n");
440 ret = RDAFM_read(chipAddr, 0x02, &tRegValue);
443 FM_ALERT("RDAFM_enable_hmute read register failed!\n");
447 tRegValue &= (~(1 << 14));
449 ret = RDAFM_write(chipAddr, 0x02, tRegValue);
453 FM_ALERT("RDAFM_enable_hmute write register failed!\n");
461 static bool RDAFM_Scan(uint8_t chipAddr,
462 uint16_t min_freq, uint16_t max_freq,
465 uint16_t *ScanTBLsize,
469 uint16_t tFreq, tRegValue = 0;
470 uint16_t tmp_scanTBLsize = *ScanTBLsize;
472 bool isTrueStation = false;
473 uint16_t oldValue = 0;
476 if((!pScanTBL) || (tmp_scanTBLsize == 0)) {
480 //clear the old value of pScanTBL
481 memset(pScanTBL, 0, sizeof(uint16_t)*RDA599X_SCANTBL_SIZE);
483 if(tmp_scanTBLsize > RDA599X_SCANTBL_SIZE)
485 tmp_scanTBLsize = RDA599X_SCANTBL_SIZE;
489 if(scandir == RDA599X_FM_SCAN_UP){ // now, only support scan up
492 tFreq = max_freq;//max_freq compare need or not
496 RDAFM_enable_hmute(chipAddr);
500 RDAFM_read(chipAddr, 0x05, &tRegValue);
501 tRegValue &= (~(0x7f<<8));
502 tRegValue |= ((0x8 & 0x7f) << 8);
503 RDAFM_write(chipAddr, 0x05, tRegValue);
506 atomic_set(&scan_complete_flag, 1);
509 if(atomic_read(&scan_complete_flag) == 0)
511 isTrueStation = false;
513 //set channel and enable TUNE
515 RDAFM_read(chipAddr, 0x03, &tRegValue);
516 tRegValue &= (~(0x03ff<<6)); //clear bit[15:6]
517 channel = tFreq - min_freq;
518 tRegValue |= ((channel << 6) | (1 << 4)); //set bit[15:6] and bit[4]
519 ret = RDAFM_write(chipAddr, 0x03, tRegValue);
522 //read 0x0B and check FM_TRUE(bit[8])
524 ret = RDAFM_read(chipAddr, 0x0B, &tRegValue);
526 if((tRegValue & 0x0100) == 0x0100){
527 isTrueStation = true;
531 //if this freq is a true station, read the channel
534 //RDAFM_read(chipAddr, 0x0A, &tRegValue);
535 //channel = ((tRegValue) & 0x03ff) - 5;
536 channel = channel - 5;
537 if((channel >= 0) && (channel != 85)){
538 oldValue = *(pScanTBL+(channel/16));
539 oldValue |= (1<<(channel%16));
540 *(pScanTBL+(channel/16)) = oldValue;
546 }while( tFreq <= max_freq );
548 #if defined(MTK_MT6515) && defined(MT6626)
549 *(pScanTBL+13) = 0xb2d4;
550 *(pScanTBL+14) = 0xb2d4;
551 *(pScanTBL+15) = 0xb2d4;
554 *ScanTBLsize = tmp_scanTBLsize;
558 RDAFM_clear_hmute(chipAddr);
564 static int fm_setup_cdev(struct fm *fm)
568 err = alloc_chrdev_region(&fm->dev_t, 0, 1, FM_NAME);
570 FM_ALERT("alloc dev_t failed\n");
574 FM_ALERT("alloc %s:%d:%d\n", FM_NAME,
575 MAJOR(fm->dev_t), MINOR(fm->dev_t));
577 cdev_init(&fm->cdev, &fm_ops);
579 fm->cdev.owner = THIS_MODULE;
580 fm->cdev.ops = &fm_ops;
582 err = cdev_add(&fm->cdev, fm->dev_t, 1);
584 FM_ALERT("alloc dev_t failed\n");
588 fm->cls = class_create(THIS_MODULE, FM_NAME);
589 if (IS_ERR(fm->cls)) {
590 err = PTR_ERR(fm->cls);
591 FM_ALERT("class_create err:%d\n", err);
594 fm->dev = device_create(fm->cls, NULL, fm->dev_t, NULL, FM_NAME);
601 static int fm_ops_ioctl(struct file *filp,
602 unsigned int cmd, unsigned long arg)
605 struct fm *fm = container_of(filp->f_dentry->d_inode->i_cdev, struct fm, cdev);
607 FM_DEBUG("%s cmd(%x)\n", __func__, cmd);
611 case FM_IOCTL_POWERUP:
613 struct fm_tune_parm parm;
614 FM_DEBUG("FM_IOCTL_POWERUP\n");
616 if (copy_from_user(&parm, (void*)arg, sizeof(struct fm_tune_parm)))
619 if (down_interruptible(&fm_ops_mutex))
621 ret = fm_powerup(fm, &parm);
623 if (copy_to_user((void*)arg, &parm, sizeof(struct fm_tune_parm)))
628 case FM_IOCTL_POWERDOWN:
630 FM_DEBUG("FM_IOCTL_POWERDOWN\n");
631 if (down_interruptible(&fm_ops_mutex))
633 ret = fm_powerdown(fm);
638 // tune (frequency, auto Hi/Lo ON/OFF )
641 struct fm_tune_parm parm;
642 FM_DEBUG("FM_IOCTL_TUNE\n");
644 if (copy_from_user(&parm, (void*)arg, sizeof(struct fm_tune_parm)))
647 if (down_interruptible(&fm_ops_mutex))
649 ret = fm_tune(fm, &parm);
652 if (copy_to_user((void*)arg, &parm, sizeof(struct fm_tune_parm)))
660 struct fm_seek_parm parm;
661 FM_DEBUG("FM_IOCTL_SEEK\n");
663 if (copy_from_user(&parm, (void*)arg, sizeof(struct fm_seek_parm)))
666 if (down_interruptible(&fm_ops_mutex))
668 ret = fm_seek(fm, &parm);
671 if (copy_to_user((void*)arg, &parm, sizeof(struct fm_seek_parm)))
677 case FM_IOCTL_SETVOL:
680 FM_DEBUG("FM_IOCTL_SETVOL\n");
682 if(copy_from_user(&vol, (void*)arg, sizeof(uint32_t))) {
683 FM_ALERT("copy_from_user failed\n");
687 if (down_interruptible(&fm_ops_mutex))
689 ret = fm_setvol(fm, vol);
695 case FM_IOCTL_GETVOL:
698 FM_DEBUG("FM_IOCTL_GETVOL\n");
700 if (down_interruptible(&fm_ops_mutex))
702 ret = fm_getvol(fm, &vol);
705 if (copy_to_user((void*)arg, &vol, sizeof(uint32_t)))
714 FM_DEBUG("FM_IOCTL_MUTE\n");
716 if (copy_from_user(&bmute, (void*)arg, sizeof(uint32_t)))
718 FM_DEBUG("copy_from_user mute failed!\n");
722 FM_DEBUG("FM_IOCTL_MUTE:%d\n", bmute);
723 if (down_interruptible(&fm_ops_mutex))
727 ret = RDAFM_enable_hmute(fm->chipAddr);
729 ret = RDAFM_clear_hmute(fm->chipAddr);
737 case FM_IOCTL_GETRSSI:
740 FM_DEBUG("FM_IOCTL_GETRSSI\n");
742 if (down_interruptible(&fm_ops_mutex))
745 ret = fm_getrssi(fm, &rssi);
748 if (copy_to_user((void*)arg, &rssi, sizeof(uint32_t)))
754 case FM_IOCTL_RW_REG:
756 struct fm_ctl_parm parm_ctl;
757 FM_DEBUG("FM_IOCTL_RW_REG\n");
759 if (copy_from_user(&parm_ctl, (void*)arg, sizeof(struct fm_ctl_parm)))
762 if (down_interruptible(&fm_ops_mutex))
765 if(parm_ctl.rw_flag == 0) //write
767 ret = RDAFM_write(fm->chipAddr, parm_ctl.addr, parm_ctl.val);
771 ret = RDAFM_read(fm->chipAddr, parm_ctl.addr, &parm_ctl.val);
775 if ((parm_ctl.rw_flag == 0x01) && (!ret)) // Read success.
777 if (copy_to_user((void*)arg, &parm_ctl, sizeof(struct fm_ctl_parm)))
783 case FM_IOCTL_GETCHIPID:
787 if (down_interruptible(&fm_ops_mutex))
790 RDAFM_GetChipID(fm->chipAddr, &chipid);
791 //chipid = fm->chip_id;
793 FM_DEBUG("FM_IOCTL_GETCHIPID:%04x\n", chipid);
796 if (copy_to_user((void*)arg, &chipid, sizeof(uint16_t)))
802 case FM_IOCTL_IS_FM_POWERED_UP:
805 FM_DEBUG("FM_IOCTL_IS_FM_POWERED_UP");
811 if (copy_to_user((void*)arg, &powerup, sizeof(uint32_t)))
817 case FM_IOCTL_DUMP_REG:
820 if (down_interruptible(&fm_ops_mutex))
822 RDAFM_GetChipID(fm->chipAddr, &chipid);
831 struct fm_scan_parm parm;
832 FM_DEBUG("FM_IOCTL_SCAN\n");
833 if (false == fm->powerup){
836 if(copy_from_user(&parm, (void*)arg, sizeof(struct fm_scan_parm))){
839 if (down_interruptible(&fm_ops_mutex)){
845 if(copy_to_user((void*)arg, &parm, sizeof(struct fm_scan_parm))){
852 case FM_IOCTL_STOP_SCAN:
854 FM_DEBUG("FM_IOCTL_STOP_SCAN\n");
858 case FM_IOCTL_SCAN_GETRSSI:
860 FM_DEBUG("FM_IOCTL_SCAN_GETRSSI\n");
864 case FM_IOCTL_GETMONOSTERO:
866 FM_DEBUG("FM_IOCTL_GETMONOSTERO\n");
870 case FM_IOCTL_SETMONOSTERO:
872 FM_DEBUG("FM_IOCTL_SETMONOSTERO\n");
876 case FM_IOCTL_GETCURPAMD:
878 FM_DEBUG("FM_IOCTL_GETCURPAMD\n");
882 case FM_IOCTL_EM_TEST:
884 FM_DEBUG("FM_IOCTL_EM_TEST\n");
888 case FM_IOCTL_RDS_SUPPORT:
890 FM_DEBUG("FM_IOCTL_RDS_SUPPORT\n");
894 case FM_IOCTL_RDS_ONOFF:
896 FM_DEBUG("FM_IOCTL_RDS_ONOFF\n");
900 case FM_IOCTL_GETGOODBCNT:
902 FM_DEBUG("FM_IOCTL_GETGOODBCNT\n");
906 case FM_IOCTL_GETBADBNT:
908 FM_DEBUG("FM_IOCTL_GETBADBNT\n");
912 case FM_IOCTL_GETBLERRATIO:
914 FM_DEBUG("FM_IOCTL_GETBLERRATIO\n");
918 case FM_IOCTL_POWERUP_TX:
920 FM_DEBUG("FM_IOCTL_POWERUP_TX\n");
924 case FM_IOCTL_TUNE_TX:
926 FM_DEBUG("FM_IOCTL_TUNE_TX\n");
930 case FM_IOCTL_TX_SUPPORT:
932 FM_DEBUG("FM_IOCTL_TX_SUPPORT\n");
936 case FM_IOCTL_RDSTX_ENABLE:
938 FM_DEBUG("FM_IOCTL_RDSTX_ENABLE\n");
942 case FM_IOCTL_RDSTX_SUPPORT:
944 FM_DEBUG("FM_IOCTL_RDSTX_SUPPORT\n");
948 case FM_IOCTL_TX_SCAN:
950 FM_DEBUG("FM_IOCTL_TX_SCAN\n");
954 case FM_IOCTL_RDS_TX:
956 FM_DEBUG("FM_IOCTL_RDS_TX\n");
960 case FM_IOCTL_OVER_BT_ENABLE:
962 FM_DEBUG("FM_IOCTL_OVER_BT_ENABLE\n");
966 case FM_IOCTL_ANA_SWITCH:
968 FM_DEBUG("FM_IOCTL_ANA_SWITCH\n");
972 case FM_IOCTL_GETCAPARRAY:
974 FM_DEBUG("FM_IOCTL_GETCAPARRAY\n");
978 case FM_IOCTL_GPS_RTC_DRIFT:
980 FM_DEBUG("FM_IOCTL_GPS_RTC_DRIFT\n");
984 case FM_IOCTL_I2S_SETTING:
986 FM_DEBUG("FM_IOCTL_I2S_SETTING\n");
990 case FM_IOCTL_RDS_GROUPCNT:
992 FM_DEBUG("FM_IOCTL_RDS_GROUPCNT\n");
996 case FM_IOCTL_RDS_GET_LOG:
998 FM_DEBUG("FM_IOCTL_RDS_GET_LOG\n");
1002 case FM_IOCTL_GET_HW_INFO:
1004 FM_DEBUG("FM_IOCTL_GET_HW_INFO\n");
1009 FM_DEBUG("default\n");
1016 static loff_t fm_ops_lseek(struct file *filp, loff_t off, int whence)
1018 // struct fm *fm = filp->private_data;
1020 if(whence == SEEK_END){
1021 //fm_hwscan_stop(fm);
1022 atomic_set(&scan_complete_flag, 0);
1023 }else if(whence == SEEK_SET){
1024 //FM_EVENT_SEND(fm->rds_event, FM_RDS_DATA_READY);
1029 static int fm_ops_open(struct inode *inode, struct file *filp)
1031 struct fm *fm = container_of(inode->i_cdev, struct fm, cdev);
1033 FM_DEBUG("%s\n", __func__);
1035 if (down_interruptible(&fm_ops_mutex))
1038 // TODO: only have to set in the first time?
1045 filp->private_data = fm;
1047 // TODO: check open flags
1052 static int fm_ops_release(struct inode *inode, struct file *filp)
1055 struct fm *fm = container_of(inode->i_cdev, struct fm, cdev);
1057 FM_DEBUG("%s\n", __func__);
1059 if (down_interruptible(&fm_ops_mutex))
1063 if(fm->powerup == true) {
1073 static int fm_init(void)
1076 struct fm *fm = NULL;
1080 FM_DEBUG("%s()\n", __func__);
1081 if (!(fm = kzalloc(sizeof(struct fm), GFP_KERNEL)))
1083 FM_ALERT("-ENOMEM\n");
1089 fm->powerup = false;
1090 fm->chipAddr = RDAFM_SLAVE_ADDR;
1091 atomic_set(&scan_complete_flag, 0);
1093 // First, read 5802NM chip ID
1094 FM_DEBUG("%s()First, read 5802NM chip ID\n", __func__);
1095 ret = RDAFM_GetChipID(fm->chipAddr, &RDAFM_CHIP_ID);
1096 FM_DEBUG("%s() 5802NM chip ID = 0x%04x\n", __func__, RDAFM_CHIP_ID);
1097 // if failed, means use FM in 5990P_E
1099 // enable the FM chip in combo
1100 FM_DEBUG("%s() enable the FM chip in combo\n", __func__);
1101 ret = rda_fm_power_on();
1107 ret = RDAFM_GetChipID(fm->chipAddr, &RDAFM_CHIP_ID);
1108 FM_DEBUG("%s() the FM in combo chip ID = 0x%04x\n", __func__, RDAFM_CHIP_ID);
1113 fm->chip_id = RDAFM_CHIP_ID;
1116 // disable the FM chip for power saving
1117 ret = rda_fm_power_off();
1123 fm->chip_id = RDAFM_CHIP_ID;
1128 if ((err = fm_setup_cdev(fm)))
1135 /***********Add porc file system*************/
1137 g_fm_proc = create_proc_entry(FM_PROC_FILE, 0444, NULL);
1138 if (g_fm_proc == NULL) {
1139 FM_ALERT("create_proc_entry failed\n");
1143 g_fm_proc->read_proc = fm_proc_read;
1144 g_fm_proc->write_proc = NULL;
1145 //g_fm_proc->owner = THIS_MODULE;
1146 FM_ALERT("create_proc_entry success\n");
1149 /********************************************/
1151 FM_DEBUG("fm_init is ok!\n");
1161 static int fm_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data)
1164 struct fm *fm = g_fm_struct;
1165 FM_ALERT("Enter fm_proc_read.\n");
1168 if (fm != NULL && fm->powerup) {
1169 cnt = sprintf(page, "1\n");
1171 cnt = sprintf(page, "0\n");
1174 FM_ALERT("Leave fm_proc_read. cnt = %d\n", cnt);
1179 static int fm_destroy(struct fm *fm)
1183 FM_DEBUG("%s\n", __func__);
1185 device_destroy(fm->cls, fm->dev_t);
1186 class_destroy(fm->cls);
1188 cdev_del(&fm->cdev);
1189 unregister_chrdev_region(fm->dev_t, 1);
1193 /***********************************/
1194 remove_proc_entry(FM_PROC_FILE, NULL);
1196 /**********************************/
1198 // FIXME: any other hardware configuration ?
1209 static int fm_powerup(struct fm *fm, struct fm_tune_parm *parm)
1212 uint16_t tRegValue = 0x0002;
1218 parm->err = FM_BADSTATUS;
1222 // if chip_id is ID_RDA5820NS, enable the FM chip in combo
1223 if(fm->chip_id == ID_RDA5820NS){
1224 ret = rda_fm_power_on();
1234 RDAFM_write(fm->chipAddr, 0x02, tRegValue);
1239 if (ID_RDA5802N == RDAFM_CHIP_ID){
1240 for (i=0; i<((sizeof(RDA5802N_initialization_reg)) / (sizeof(uint16_t))); i++)
1242 ret = RDAFM_write(fm->chipAddr, i+2, RDA5802N_initialization_reg[i]);
1246 FM_DEBUG("fm_powerup init failed!\n");
1248 parm->err = FM_FAILED;
1254 }else if (ID_RDA5820NS == RDAFM_CHIP_ID){
1255 if(RDA_RADIO_WorkType == FM_RECEIVER){
1256 for (i = 0; i < ((sizeof(RDA5820NS_RX_initialization_reg)) / (sizeof(RDA_FM_REG_T))); i++)
1258 if(RDA5820NS_RX_initialization_reg[i].address == 0xFF){
1259 msleep(RDA5820NS_RX_initialization_reg[i].value);
1261 ret = RDAFM_write(fm->chipAddr, RDA5820NS_RX_initialization_reg[i].address, RDA5820NS_RX_initialization_reg[i].value);
1264 FM_DEBUG("fm_powerup init failed!\n");
1265 parm->err = FM_FAILED;
1271 for (i = 0; i < ((sizeof(RDA5820NS_TX_initialization_reg)) / (sizeof(RDA_FM_REG_T))); i++)
1273 if(RDA5820NS_TX_initialization_reg[i].address == 0xFF){
1274 msleep(RDA5820NS_TX_initialization_reg[i].value);
1276 ret = RDAFM_write(fm->chipAddr, RDA5820NS_TX_initialization_reg[i].address, RDA5820NS_TX_initialization_reg[i].value);
1279 FM_DEBUG("fm_powerup init failed!\n");
1280 parm->err = FM_FAILED;
1290 FM_DEBUG("pwron ok\n");
1293 if (fm_tune(fm, parm) < 0)
1297 fm_tune_data.byPowerUp = true;
1298 memcpy(&fm_tune_data.parm, parm, sizeof(fm_tune_data.parm));
1300 parm->err = FM_SUCCESS;
1309 static int fm_powerdown(struct fm *fm)
1311 uint16_t tRegValue = 0;
1314 RDAFM_read(fm->chipAddr, 0x02, &tRegValue);
1315 tRegValue &= (~(1 << 0));
1316 RDAFM_write(fm->chipAddr, 0x02, tRegValue);
1318 if(fm->chip_id == ID_RDA5820NS){
1319 ret = rda_fm_power_off();
1325 fm->powerup = false;
1326 FM_ALERT("pwrdown ok\n");
1334 static int fm_seek(struct fm *fm, struct fm_seek_parm *parm)
1339 uint16_t tFreq = 875;
1340 uint16_t tRegValue = 0;
1341 uint16_t bottomOfBand = 875;
1342 int falseStation = -1;
1347 parm->err = FM_BADSTATUS;
1351 if (parm->space == FM_SPACE_100K)
1354 val &= (~((1<<0) | (1<<1)));
1356 else if (parm->space == FM_SPACE_200K)
1364 parm->err = FM_EPARM;
1368 if (parm->band == FM_BAND_UE)
1370 val &= (~((1<<2) | (1<<3)));
1373 fm->max_freq = 1080;
1375 else if (parm->band == FM_BAND_JAPAN)
1383 else if (parm->band == FM_BAND_JAPANW) {
1388 fm->max_freq = 1080;
1392 FM_ALERT("band:%d out of range\n", parm->band);
1393 parm->err = FM_EPARM;
1397 if (parm->freq < fm->min_freq || parm->freq > fm->max_freq) {
1398 FM_ALERT("freq:%d out of range\n", parm->freq);
1399 parm->err = FM_EPARM;
1403 if (parm->seekth > 0x0B) {
1404 FM_ALERT("seekth:%d out of range\n", parm->seekth);
1405 parm->err = FM_EPARM;
1409 RDAFM_read(fm->chipAddr, 0x05, &tRegValue);
1410 tRegValue &= (~(0x7f<<8));
1411 //tRegValue |= ((parm->seekth & 0x7f) << 8);
1412 tRegValue |= ((0x8 & 0x7f) << 8);
1413 RDAFM_write(fm->chipAddr, 0x05, tRegValue);
1417 if (parm->seekdir == FM_SEEK_UP)
1418 FM_DEBUG("seek %d up\n", parm->freq);
1420 FM_DEBUG("seek %d down\n", parm->freq);
1423 // (1) set hmute bit
1424 RDAFM_enable_hmute(fm->chipAddr);
1430 if (parm->seekdir == FM_SEEK_UP)
1435 if (tFreq > fm->max_freq)
1436 tFreq = fm->min_freq;
1437 if (tFreq < fm->min_freq)
1438 tFreq = fm->max_freq;
1440 val = (((tFreq - bottomOfBand+5) << 6) | (1 << 4) | (val & 0x0f));
1441 RDAFM_write(fm->chipAddr, 0x03, val);
1443 ret = RDAFM_read(fm->chipAddr, 0x0B, &tRegValue);
1446 FM_DEBUG("fm_seek: read register failed tunning freq = %4X\n", tFreq);
1451 if ((tRegValue & 0x0100) == 0x0100)
1457 if(falseStation == 0)
1460 }while(tFreq != parm->freq);
1464 RDAFM_clear_hmute(fm->chipAddr);
1466 if (falseStation == 0) // seek successfully
1469 FM_ALERT("fm_seek success, freq:%d\n", parm->freq);
1470 parm->err = FM_SUCCESS;
1475 FM_ALERT("fm_seek failed, invalid freq\n");
1476 parm->err = FM_SEEK_FAILED;
1486 static int fm_scan(struct fm *fm, struct fm_scan_parm *parm)
1489 uint16_t tRegValue = 0;
1490 uint16_t scandir = RDA599X_FM_SCAN_UP; //scandir ËÑË÷·½Ïò
1494 parm->err = FM_BADSTATUS;
1498 RDAFM_read(fm->chipAddr, 0x03, &tRegValue);
1500 if (parm->space == FM_SPACE_100K){
1502 tRegValue &= (~((1<<0) | (1<<1))); //set 03H's bit[1:0] to 00
1503 }else if (parm->space == FM_SPACE_200K) {
1505 tRegValue &= (~(1<<1)); //clear bit[1]
1506 tRegValue |= (1<<0); //set bit[0]
1510 tRegValue &= (~((1<<0) | (1<<1))); //set 03H's bit[1:0] to 00
1513 if(parm->band == FM_BAND_UE){
1514 tRegValue &= (~((1<<2) | (1<<3)));
1516 fm->max_freq = 1080;
1517 }else if(parm->band == FM_BAND_JAPAN){
1518 tRegValue &= (~(1<<3));
1519 tRegValue |= (1 << 2);
1522 }else if(parm->band == FM_BAND_JAPANW){
1523 tRegValue &= (~(1<<2));
1524 tRegValue |= (1 << 3);
1526 fm->max_freq = 1080;
1528 parm->err = FM_EPARM;
1532 //set space and band
1533 RDAFM_write(fm->chipAddr, 0x03, tRegValue);
1537 if(RDAFM_Scan(fm->chipAddr, fm->min_freq, fm->max_freq, &(parm->freq), parm->ScanTBL, &(parm->ScanTBLSize), scandir, space)){
1538 parm->err = FM_SUCCESS;
1540 parm->err = FM_SEEK_FAILED;
1547 static int fm_setvol(struct fm *fm, uint32_t vol)
1550 uint16_t tRegValue = 0;
1555 FM_DEBUG("fm_setvol:%d\n", vol);
1557 ret = RDAFM_read(fm->chipAddr, 0x05, &tRegValue);
1560 tRegValue &= ~(0x000f);
1563 ret = RDAFM_write(fm->chipAddr, 0x05, tRegValue);
1570 static int fm_getvol(struct fm *fm, uint32_t *vol)
1575 ret = RDAFM_read(fm->chipAddr, 0x05, &tRegValue);
1582 *vol = (tRegValue & 0x000F);
1587 static int fm_getrssi(struct fm *fm, uint32_t *rssi)
1592 ret = RDAFM_read(fm->chipAddr, 0x0B, &tRegValue);
1597 *rssi = (uint32_t)((tRegValue >> 9) & RDAFM_MASK_RSSI);
1599 FM_DEBUG("rssi value:%d\n", *rssi);
1607 static int fm_tune(struct fm *fm, struct fm_tune_parm *parm)
1612 uint16_t bottomOfBand = 875;
1615 FM_DEBUG("%s\n", __func__);
1619 parm->err = FM_BADSTATUS;
1623 if (parm->space == FM_SPACE_100K)
1626 val &= (~((1<<0) | (1<<1)));
1628 else if (parm->space == FM_SPACE_200K)
1636 parm->err = FM_EPARM;
1640 if (parm->band == FM_BAND_UE)
1642 val &= (~((1<<2) | (1<<3)));
1645 fm->max_freq = 1080;
1647 else if (parm->band == FM_BAND_JAPAN)
1655 else if (parm->band == FM_BAND_JAPANW) {
1660 fm->max_freq = 1080;
1664 FM_ALERT("band:%d out of range\n", parm->band);
1665 parm->err = FM_EPARM;
1669 if (parm->freq < fm->min_freq || parm->freq > fm->max_freq) {
1670 FM_ALERT("freq:%d out of range\n", parm->freq);
1671 parm->err = FM_EPARM;
1675 FM_DEBUG("fm_tune, freq:%d\n", parm->freq);
1678 val = (((parm->freq - bottomOfBand + 5) << 6) | (1 << 4) | (val & 0x0f));
1680 ret = RDAFM_write(fm->chipAddr, 0x03, val);
1683 FM_ALERT("fm_tune write freq failed\n");
1684 parm->err = FM_SEEK_FAILED;
1693 static int mt_fm_probe(struct platform_device *pdev)
1696 FM_DEBUG("mt_fm_probe\n");
1698 if ((err = fm_init()))
1700 FM_ALERT("fm_init ERR:%d\n", err);
1706 static int mt_fm_remove(struct platform_device *pdev)
1708 FM_DEBUG("mt_fm_remove\n");
1710 struct fm *fm = g_fm_struct;
1721 static struct platform_driver mt_fm_dev_drv =
1723 .probe = mt_fm_probe,
1724 .remove = mt_fm_remove,
1727 .owner = THIS_MODULE,
1731 #if defined(MTK_MT6515)
1732 static struct platform_device mt_fm_device = {
1742 static int __init mt_fm_init(void)
1746 FM_DEBUG("mt_fm_init\n");
1747 #if defined(MTK_MT6515)
1748 err = platform_device_register(&mt_fm_device);
1750 FM_DEBUG("platform_device_register fail\n");
1753 FM_DEBUG("platform_device_register success\n");
1756 err = platform_driver_register(&mt_fm_dev_drv);
1759 FM_DEBUG("platform_driver_register failed\n");
1761 FM_DEBUG("platform_driver_register success\n");
1770 static void __exit mt_fm_exit(void)
1772 FM_DEBUG("mt_fm_exit\n");
1773 platform_driver_unregister(&mt_fm_dev_drv);
1774 #if defined(MTK_MT6515)
1775 platform_device_unregister(&mt_fm_device);
1779 module_init(mt_fm_init);
1780 module_exit(mt_fm_exit);
1782 MODULE_LICENSE("GPL");
1783 MODULE_DESCRIPTION("MediaTek FM Driver");
1784 MODULE_AUTHOR("William Chung <William.Chung@MediaTek.com>");