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_fm_power_off();
48 extern int rda_fm_power_on();
50 /******************************************************************************
51 * CONSTANT DEFINITIONS
52 *****************************************************************************/
53 #define RDAFM_SLAVE_ADDR (0x11 << 1) //RDA FM Chip address
55 #define RDAFM_MASK_RSSI 0X7F // RSSI
56 #define RDAFM_DEV "RDA599x"
58 //customer need customize the I2C port
59 #define RDAFM_I2C_PORT 0
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
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;
119 uint16_t min_freq; // KHz
120 uint16_t max_freq; // KHz
121 uint8_t band; // TODO
125 struct i2c_client *i2c_client;
131 /******************************************************************************
132 * FUNCTION PROTOTYPES
133 *****************************************************************************/
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
148 uint16_t *ScanTBLsize,
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);
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);
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);
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);
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);
185 /******************************************************************************
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;
193 static struct i2c_client_address_data RDAFM_addr_data = {
194 .normal_i2c = normal_i2c,
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))};
206 static struct i2c_driver RDAFM_driver = {
207 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31))
209 .owner = THIS_MODULE,
212 .attach_adapter = fm_i2c_attach_adapter,
213 .detach_client = fm_i2c_detach_client,
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,
225 static uint16_t RDAFM_CHIP_ID = 0x5808;
226 static RDA_RADIO_WORK_E RDA_RADIO_WorkType = FM_RECEIVER;
231 static uint16_t RDA5802N_initialization_reg[]={
235 0xC6ED, //0x86AD, //05h
257 0xF7CF, //Ìá¸ßÔ¶¶ËÔëÉùÒÖÖÆ
258 0x2414, //0x2ADC, //0x1ch ÌáÉýVIO VDDÖ®¼äѹ²îÒýÆðµÄ²»Á¼
270 static uint16_t RDA5802N_initialization_reg[]={
310 static RDA_FM_REG_T RDA5820NS_TX_initialization_reg[]={
312 {0xFF, 100}, // if address is 0xFF, sleep value ms
323 static RDA_FM_REG_T RDA5820NS_RX_initialization_reg[]={
324 {0x02, 0x0002}, //Soft reset
326 {0x02, 0xC001}, //Power Up
327 {0x05, 0x888F}, //LNAP 0x884F --LNAN
338 static struct file_operations fm_ops = {
339 .owner = THIS_MODULE,
340 .unlocked_ioctl = fm_ops_ioctl,
341 .llseek = fm_ops_lseek,
343 .release = fm_ops_release,
346 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
347 static DECLARE_MUTEX(fm_ops_mutex);
349 DEFINE_SEMAPHORE(fm_ops_mutex);
352 /******************************************************************************
353 *****************************************************************************/
355 /******************************************************************************
356 *****************************************************************************/
360 static int RDAFM_GetChipID(struct i2c_client *client, uint16_t *pChipID)
364 uint16_t val = 0x0002;
367 err = RDAFM_write(client, 0x02, val);
370 FM_DEBUG("RDAFM_GetChipID: reset FM chip failed!\n");
378 err = RDAFM_read(client, 0x0C, &val);
381 if ((0x5802 == val) || (0x5803 == val))
383 err = RDAFM_read(client, 0x0E, &val);
391 FM_DEBUG("RDAFM_GetChipID: Chip ID = %04X\n", val);
397 else if ((0x5805 == val) || (0x5820 == val))
405 FM_DEBUG("RDAFM_GetChipID: get chip ID failed! get value = %04X\n", val);
414 FM_DEBUG("RDAFM_GetChipID: get chip ID failed!\n");
426 static int RDAFM_read(struct i2c_client *client, uint8_t addr, uint16_t *val)
431 // first, send addr to RDAFM
432 n = i2c_master_send(client, (char*)&addr, 1);
435 FM_ALERT("RDAFM_read send:0x%X err:%d\n", addr, n);
439 // second, receive two byte from RDAFM
440 n = i2c_master_recv(client, b, 2);
443 FM_ALERT("RDAFM_read recv:0x%X err:%d\n", addr, n);
447 *val = (uint16_t)(b[0] << 8 | b[1]);
455 static int RDAFM_write(struct i2c_client *client, uint8_t addr, uint16_t val)
461 b[1] = (char)(val >> 8);
462 b[2] = (char)(val & 0xFF);
464 n = i2c_master_send(client, b, 3);
467 FM_ALERT("RDAFM_write send:0x%X err:%d\n", addr, n);
475 static int RDAFM_clear_hmute(struct i2c_client *client)
478 uint16_t tRegValue = 0;
480 FM_DEBUG("RDAFM_clear_hmute\n");
482 ret = RDAFM_read(client, 0x02, &tRegValue);
485 FM_ALERT("RDAFM_clear_hmute read register failed!\n");
489 tRegValue |= (1 << 14);
491 ret = RDAFM_write(client, 0x02, tRegValue);
495 FM_ALERT("RDAFM_clear_hmute write register failed!\n");
499 if(fm_tune_data.byPowerUp){
500 if (fm_tune(g_fm_struct, &(fm_tune_data.parm)) < 0)
502 fm_tune_data.byPowerUp = false;
503 memset(&fm_tune_data.parm, 0, sizeof(fm_tune_data.parm));
506 fm_tune_data.byPowerUp = false;
507 memset(&fm_tune_data.parm, 0, sizeof(fm_tune_data.parm));
515 static int RDAFM_enable_hmute(struct i2c_client *client)
518 uint16_t tRegValue = 0;
520 FM_DEBUG("RDAFM_enable_hmute\n");
522 ret = RDAFM_read(client, 0x02, &tRegValue);
525 FM_ALERT("RDAFM_enable_hmute read register failed!\n");
529 tRegValue &= (~(1 << 14));
531 ret = RDAFM_write(client, 0x02, tRegValue);
535 FM_ALERT("RDAFM_enable_hmute write register failed!\n");
544 static int RDAFM_clear_tune(struct i2c_client *client)
552 static int RDAFM_enable_tune(struct i2c_client *client)
560 static int RDAFM_clear_seek(struct i2c_client *client)
568 static int RDAFM_enable_seek(struct i2c_client *client)
575 //b=true set stereo else set mono
576 static int RDAFM_SetStereo(struct i2c_client *client, uint8_t b)
579 uint16_t tRegValue = 0;
581 FM_DEBUG("RDAFM_SetStereo\n");
583 ret = RDAFM_read(client, 0x02, &tRegValue);
586 FM_ALERT("RDAFM_SetStereo read register failed!\n");
590 tRegValue &= (~(1 << 13));//set stereo
592 tRegValue |= (1 << 13); //set mono
594 ret = RDAFM_write(client, 0x02, tRegValue);
598 FM_ALERT("RDAFM_SetStereo write register failed!\n");
608 static int RDAFM_SetRSSI_Threshold(struct i2c_client *client, uint8_t RssiThreshold)
611 uint16_t tRegValue = 0;
613 FM_DEBUG("RDAFM_SetRSSI_Threshold\n");
615 ret = RDAFM_read(client, 0x05, &tRegValue);
618 FM_ALERT("RDAFM_SetRSSI_Threshold read register failed!\n");
622 tRegValue &= 0x80FF;//clear valume
623 tRegValue |= ((RssiThreshold & 0x7f) << 8); //set valume
625 ret = RDAFM_write(client, 0x05, tRegValue);
629 FM_ALERT("RDAFM_SetRSSI_Threshold write register failed!\n");
640 static int RDAFM_SetDe_Emphasis(struct i2c_client *client, uint8_t index)
643 uint16_t tRegValue = 0;
645 FM_DEBUG("RDAFM_SetRSSI_Threshold\n");
647 ret = RDAFM_read(client, 0x04, &tRegValue);
650 FM_ALERT("RDAFM_SetRSSI_Threshold read register failed!\n");
656 tRegValue &= (~(1 << 11));//De_Emphasis=75us
660 tRegValue |= (1 << 11);//De_Emphasis=50us
664 ret = RDAFM_write(client, 0x04, tRegValue);
668 FM_ALERT("RDAFM_SetRSSI_Threshold write register failed!\n");
679 static void RDAFM_em_test(struct i2c_client *client, uint16_t group_idx, uint16_t item_idx, uint32_t item_value)
681 FM_ALERT("RDAFM_em_test %d:%d:%d\n", group_idx, item_idx, item_value);
687 RDAFM_SetStereo(client, 0); //force mono
691 RDAFM_SetStereo(client, 1); //stereo
699 RDAFM_SetStereo(client, 1); //stereo
703 RDAFM_SetStereo(client, 0); //force mono
708 RDAFM_SetRSSI_Threshold(client, item_value);
710 case Softmute_Enable:
713 RDAFM_enable_hmute(client);
717 RDAFM_clear_hmute(client);
721 if(item_idx >= 2) //0us
723 FM_ALERT("RDAFM not support De_emphasis 0\n");
727 RDAFM_SetDe_Emphasis(client,item_idx);//0=75us,1=50us
735 FM_ALERT("RDAFM not support this setting\n");
740 static bool RDAFM_Scan(struct i2c_client *client,
741 uint16_t min_freq, uint16_t max_freq,
744 uint16_t *ScanTBLsize,
748 uint16_t tFreq, tRegValue = 0;
749 uint16_t tmp_scanTBLsize = *ScanTBLsize;
751 bool isTrueStation = false;
752 uint16_t oldValue = 0;
755 if((!pScanTBL) || (tmp_scanTBLsize == 0)) {
759 //clear the old value of pScanTBL
760 memset(pScanTBL, 0, sizeof(uint16_t)*RDA599X_SCANTBL_SIZE);
762 if(tmp_scanTBLsize > RDA599X_SCANTBL_SIZE)
764 tmp_scanTBLsize = RDA599X_SCANTBL_SIZE;
768 if(scandir == RDA599X_FM_SCAN_UP){ // now, only support scan up
771 tFreq = max_freq;//max_freq compare need or not
775 RDAFM_enable_hmute(client);
779 RDAFM_read(client, 0x05, &tRegValue);
780 tRegValue &= (~(0x7f<<8));
781 tRegValue |= ((0x8 & 0x7f) << 8);
782 RDAFM_write(client, 0x05, tRegValue);
785 atomic_set(&scan_complete_flag, 1);
787 if(atomic_read(&scan_complete_flag) == 0)
789 isTrueStation = false;
791 //set channel and enable TUNE
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);
800 //read 0x0B and check FM_TRUE(bit[8])
802 ret = RDAFM_read(client, 0x0B, &tRegValue);
804 if((tRegValue & 0x0100) == 0x0100){
805 isTrueStation = true;
809 //if this freq is a true station, read the channel
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;
824 }while( tFreq <= max_freq );
826 #if defined(MTK_MT6515) && defined(MT6626)
827 *(pScanTBL+13) = 0xb2d4;
828 *(pScanTBL+14) = 0xb2d4;
829 *(pScanTBL+15) = 0xb2d4;
832 *ScanTBLsize = tmp_scanTBLsize;
836 RDAFM_clear_hmute(client);
842 static int fm_setup_cdev(struct fm *fm)
846 err = alloc_chrdev_region(&fm->dev_t, 0, 1, FM_NAME);
848 FM_ALERT("alloc dev_t failed\n");
852 FM_ALERT("alloc %s:%d:%d\n", FM_NAME,
853 MAJOR(fm->dev_t), MINOR(fm->dev_t));
855 cdev_init(&fm->cdev, &fm_ops);
857 fm->cdev.owner = THIS_MODULE;
858 fm->cdev.ops = &fm_ops;
860 err = cdev_add(&fm->cdev, fm->dev_t, 1);
862 FM_ALERT("alloc dev_t failed\n");
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);
872 fm->dev = device_create(fm->cls, NULL, fm->dev_t, NULL, FM_NAME);
879 static int fm_ops_ioctl(struct file *filp,
880 unsigned int cmd, unsigned long arg)
883 struct fm *fm = container_of(filp->f_dentry->d_inode->i_cdev, struct fm, cdev);
885 FM_DEBUG("%s cmd(%x)\n", __func__, cmd);
889 case FM_IOCTL_POWERUP:
891 struct fm_tune_parm parm;
892 FM_DEBUG("FM_IOCTL_POWERUP\n");
895 // if (!capable(CAP_SYS_ADMIN))
898 if (copy_from_user(&parm, (void*)arg, sizeof(struct fm_tune_parm)))
901 if (down_interruptible(&fm_ops_mutex))
903 ret = fm_powerup(fm, &parm);
905 if (copy_to_user((void*)arg, &parm, sizeof(struct fm_tune_parm)))
907 // fm_low_power_wa(1);
911 case FM_IOCTL_POWERDOWN:
913 FM_DEBUG("FM_IOCTL_POWERDOWN\n");
915 // if (!capable(CAP_SYS_ADMIN))
917 if (down_interruptible(&fm_ops_mutex))
919 ret = fm_powerdown(fm);
921 // fm_low_power_wa(0);
925 // tune (frequency, auto Hi/Lo ON/OFF )
928 struct fm_tune_parm parm;
929 FM_DEBUG("FM_IOCTL_TUNE\n");
931 // if (!capable(CAP_SYS_ADMIN))
934 if (copy_from_user(&parm, (void*)arg, sizeof(struct fm_tune_parm)))
937 if (down_interruptible(&fm_ops_mutex))
939 ret = fm_tune(fm, &parm);
942 if (copy_to_user((void*)arg, &parm, sizeof(struct fm_tune_parm)))
950 struct fm_seek_parm parm;
951 FM_DEBUG("FM_IOCTL_SEEK\n");
954 // if (!capable(CAP_SYS_ADMIN))
957 if (copy_from_user(&parm, (void*)arg, sizeof(struct fm_seek_parm)))
960 if (down_interruptible(&fm_ops_mutex))
962 ret = fm_seek(fm, &parm);
965 if (copy_to_user((void*)arg, &parm, sizeof(struct fm_seek_parm)))
971 case FM_IOCTL_SETVOL:
974 FM_DEBUG("FM_IOCTL_SETVOL\n");
977 // if (!capable(CAP_SYS_ADMIN))
980 if(copy_from_user(&vol, (void*)arg, sizeof(uint32_t))) {
981 FM_ALERT("copy_from_user failed\n");
985 if (down_interruptible(&fm_ops_mutex))
987 ret = fm_setvol(fm, vol);
993 case FM_IOCTL_GETVOL:
996 FM_DEBUG("FM_IOCTL_GETVOL\n");
999 // if (!capable(CAP_SYS_ADMIN))
1002 if (down_interruptible(&fm_ops_mutex))
1004 ret = fm_getvol(fm, &vol);
1007 if (copy_to_user((void*)arg, &vol, sizeof(uint32_t)))
1016 FM_DEBUG("FM_IOCTL_MUTE\n");
1019 // if (!capable(CAP_SYS_ADMIN))
1021 if (copy_from_user(&bmute, (void*)arg, sizeof(uint32_t)))
1023 FM_DEBUG("copy_from_user mute failed!\n");
1027 FM_DEBUG("FM_IOCTL_MUTE:%d\n", bmute);
1028 if (down_interruptible(&fm_ops_mutex))
1032 ret = RDAFM_enable_hmute(fm->i2c_client);
1034 ret = RDAFM_clear_hmute(fm->i2c_client);
1042 case FM_IOCTL_GETRSSI:
1045 FM_DEBUG("FM_IOCTL_GETRSSI\n");
1048 // if (!capable(CAP_SYS_ADMIN))
1051 if (down_interruptible(&fm_ops_mutex))
1054 ret = fm_getrssi(fm, &rssi);
1057 if (copy_to_user((void*)arg, &rssi, sizeof(uint32_t)))
1063 case FM_IOCTL_RW_REG:
1065 struct fm_ctl_parm parm_ctl;
1066 FM_DEBUG("FM_IOCTL_RW_REG\n");
1069 // if (!capable(CAP_SYS_ADMIN))
1072 if (copy_from_user(&parm_ctl, (void*)arg, sizeof(struct fm_ctl_parm)))
1075 if (down_interruptible(&fm_ops_mutex))
1078 if(parm_ctl.rw_flag == 0) //write
1080 ret = RDAFM_write(fm->i2c_client, parm_ctl.addr, parm_ctl.val);
1084 ret = RDAFM_read(fm->i2c_client, parm_ctl.addr, &parm_ctl.val);
1088 if ((parm_ctl.rw_flag == 0x01) && (!ret)) // Read success.
1090 if (copy_to_user((void*)arg, &parm_ctl, sizeof(struct fm_ctl_parm)))
1096 case FM_IOCTL_GETCHIPID:
1100 if (down_interruptible(&fm_ops_mutex))
1103 RDAFM_GetChipID(fm->i2c_client, &chipid);
1104 //chipid = fm->chip_id;
1106 FM_DEBUG("FM_IOCTL_GETCHIPID:%04x\n", chipid);
1109 if (copy_to_user((void*)arg, &chipid, sizeof(uint16_t)))
1115 case FM_IOCTL_EM_TEST:
1117 struct fm_em_parm parm_em;
1118 FM_DEBUG("FM_IOCTL_EM_TEST\n");
1121 // if (!capable(CAP_SYS_ADMIN))
1124 if (copy_from_user(&parm_em, (void*)arg, sizeof(struct fm_em_parm)))
1127 if (down_interruptible(&fm_ops_mutex))
1130 RDAFM_em_test(fm->i2c_client, parm_em.group_idx, parm_em.item_idx, parm_em.item_value);
1136 case FM_IOCTL_IS_FM_POWERED_UP:
1139 FM_DEBUG("FM_IOCTL_IS_FM_POWERED_UP");
1145 if (copy_to_user((void*)arg, &powerup, sizeof(uint32_t)))
1151 case FM_IOCTL_DUMP_REG:
1153 uint16_t chipid = 0;
1154 if (down_interruptible(&fm_ops_mutex))
1156 RDAFM_GetChipID(fm->i2c_client, &chipid);
1165 struct fm_scan_parm parm;
1166 FM_DEBUG("FM_IOCTL_SCAN\n");
1167 if (false == fm->powerup){
1170 if(copy_from_user(&parm, (void*)arg, sizeof(struct fm_scan_parm))){
1173 if (down_interruptible(&fm_ops_mutex)){
1179 if(copy_to_user((void*)arg, &parm, sizeof(struct fm_scan_parm))){
1186 case FM_IOCTL_STOP_SCAN:
1188 FM_DEBUG("FM_IOCTL_STOP_SCAN\n");
1194 FM_DEBUG("default\n");
1201 static loff_t fm_ops_lseek(struct file *filp, loff_t off, int whence)
1203 // struct fm *fm = filp->private_data;
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);
1214 static int fm_ops_open(struct inode *inode, struct file *filp)
1216 struct fm *fm = container_of(inode->i_cdev, struct fm, cdev);
1218 FM_DEBUG("%s\n", __func__);
1220 if (down_interruptible(&fm_ops_mutex))
1223 // TODO: only have to set in the first time?
1230 filp->private_data = fm;
1232 // TODO: check open flags
1237 static int fm_ops_release(struct inode *inode, struct file *filp)
1240 struct fm *fm = container_of(inode->i_cdev, struct fm, cdev);
1242 FM_DEBUG("%s\n", __func__);
1244 if (down_interruptible(&fm_ops_mutex))
1248 if(fm->powerup == true) {
1258 static int fm_init(struct i2c_client *client)
1261 struct fm *fm = NULL;
1265 FM_DEBUG("%s()\n", __func__);
1266 if (!(fm = kzalloc(sizeof(struct fm), GFP_KERNEL)))
1268 FM_ALERT("-ENOMEM\n");
1274 fm->powerup = false;
1275 atomic_set(&scan_complete_flag, 0);
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
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();
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);
1297 fm->chip_id = RDAFM_CHIP_ID;
1300 // disable the FM chip for power saving
1301 ret = rda_fm_power_off();
1307 fm->chip_id = RDAFM_CHIP_ID;
1312 if ((err = fm_setup_cdev(fm)))
1318 fm->i2c_client = client;
1319 i2c_set_clientdata(client, fm);
1322 /***********Add porc file system*************/
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");
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");
1336 /********************************************/
1338 FM_DEBUG("fm_init is ok!\n");
1348 static int fm_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data)
1351 struct fm *fm = g_fm_struct;
1352 FM_ALERT("Enter fm_proc_read.\n");
1355 if (fm != NULL && fm->powerup) {
1356 cnt = sprintf(page, "1\n");
1358 cnt = sprintf(page, "0\n");
1361 FM_ALERT("Leave fm_proc_read. cnt = %d\n", cnt);
1366 static int fm_destroy(struct fm *fm)
1370 FM_DEBUG("%s\n", __func__);
1372 device_destroy(fm->cls, fm->dev_t);
1373 class_destroy(fm->cls);
1375 cdev_del(&fm->cdev);
1376 unregister_chrdev_region(fm->dev_t, 1);
1380 /***********************************/
1381 remove_proc_entry(FM_PROC_FILE, NULL);
1383 /**********************************/
1385 // FIXME: any other hardware configuration ?
1396 static int fm_powerup(struct fm *fm, struct fm_tune_parm *parm)
1399 uint16_t tRegValue = 0x0002;
1402 struct i2c_client *client = fm->i2c_client;
1406 parm->err = FM_BADSTATUS;
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();
1422 RDAFM_write(client, 0x02, tRegValue);
1426 if (ID_RDA5802N == RDAFM_CHIP_ID){
1427 for (i=0; i<((sizeof(RDA5802N_initialization_reg)) / (sizeof(uint16_t))); i++)
1429 ret = RDAFM_write(client, i+2, RDA5802N_initialization_reg[i]);
1433 FM_DEBUG("fm_powerup init failed!\n");
1435 parm->err = FM_FAILED;
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++)
1445 if(RDA5820NS_RX_initialization_reg[i].address == 0xFF){
1446 msleep(RDA5820NS_RX_initialization_reg[i].value);
1448 ret = RDAFM_write(client, RDA5820NS_RX_initialization_reg[i].address, RDA5820NS_RX_initialization_reg[i].value);
1451 FM_DEBUG("fm_powerup init failed!\n");
1452 parm->err = FM_FAILED;
1458 for (i = 0; i < ((sizeof(RDA5820NS_TX_initialization_reg)) / (sizeof(RDA_FM_REG_T))); i++)
1460 if(RDA5820NS_TX_initialization_reg[i].address == 0xFF){
1461 msleep(RDA5820NS_TX_initialization_reg[i].value);
1463 ret = RDAFM_write(client, RDA5820NS_TX_initialization_reg[i].address, RDA5820NS_TX_initialization_reg[i].value);
1466 FM_DEBUG("fm_powerup init failed!\n");
1467 parm->err = FM_FAILED;
1477 FM_DEBUG("pwron ok\n");
1480 if (fm_tune(fm, parm) < 0)
1484 fm_tune_data.byPowerUp = true;
1485 memcpy(&fm_tune_data.parm, parm, sizeof(fm_tune_data.parm));
1487 parm->err = FM_SUCCESS;
1496 static int fm_powerdown(struct fm *fm)
1498 uint16_t tRegValue = 0;
1500 struct i2c_client *client = fm->i2c_client;
1502 RDAFM_read(client, 0x02, &tRegValue);
1503 tRegValue &= (~(1 << 0));
1504 RDAFM_write(client, 0x02, tRegValue);
1506 if(fm->chip_id == ID_RDA5820NS){
1507 ret = rda_fm_power_off();
1513 fm->powerup = false;
1514 FM_ALERT("pwrdown ok\n");
1522 static int fm_seek(struct fm *fm, struct fm_seek_parm *parm)
1527 uint16_t tFreq = 875;
1528 uint16_t tRegValue = 0;
1529 uint16_t bottomOfBand = 875;
1530 int falseStation = -1;
1533 struct i2c_client *client = fm->i2c_client;
1537 parm->err = FM_BADSTATUS;
1541 if (parm->space == FM_SPACE_100K)
1544 val &= (~((1<<0) | (1<<1)));
1546 else if (parm->space == FM_SPACE_200K)
1554 parm->err = FM_EPARM;
1558 if (parm->band == FM_BAND_UE)
1560 val &= (~((1<<2) | (1<<3)));
1563 fm->max_freq = 1080;
1565 else if (parm->band == FM_BAND_JAPAN)
1573 else if (parm->band == FM_BAND_JAPANW) {
1578 fm->max_freq = 1080;
1582 FM_ALERT("band:%d out of range\n", parm->band);
1583 parm->err = FM_EPARM;
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;
1593 if (parm->seekth > 0x0B) {
1594 FM_ALERT("seekth:%d out of range\n", parm->seekth);
1595 parm->err = FM_EPARM;
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);
1607 if (parm->seekdir == FM_SEEK_UP)
1608 FM_DEBUG("seek %d up\n", parm->freq);
1610 FM_DEBUG("seek %d down\n", parm->freq);
1613 // (1) set hmute bit
1614 RDAFM_enable_hmute(client);
1619 if (parm->seekdir == FM_SEEK_UP)
1624 if (tFreq > fm->max_freq)
1625 tFreq = fm->min_freq;
1626 if (tFreq < fm->min_freq)
1627 tFreq = fm->max_freq;
1629 val = (((tFreq - bottomOfBand+5) << 6) | (1 << 4) | (val & 0x0f));
1630 RDAFM_write(client, 0x03, val);
1632 ret = RDAFM_read(client, 0x0B, &tRegValue);
1635 FM_DEBUG("fm_seek: read register failed tunning freq = %4X\n", tFreq);
1640 if ((tRegValue & 0x0100) == 0x0100)
1646 if(falseStation == 0)
1648 }while(tFreq != parm->freq);
1652 RDAFM_clear_hmute(client);
1654 if (falseStation == 0) // seek successfully
1657 FM_ALERT("fm_seek success, freq:%d\n", parm->freq);
1658 parm->err = FM_SUCCESS;
1664 FM_ALERT("fm_seek failed, invalid freq\n");
1665 parm->err = FM_SEEK_FAILED;
1675 static int fm_scan(struct fm *fm, struct fm_scan_parm *parm)
1678 uint16_t tRegValue = 0;
1679 uint16_t scandir = RDA599X_FM_SCAN_UP; //scandir ËÑË÷·½Ïò
1681 struct i2c_client *client = fm->i2c_client;
1684 parm->err = FM_BADSTATUS;
1688 RDAFM_read(client, 0x03, &tRegValue);
1690 if (parm->space == FM_SPACE_100K){
1692 tRegValue &= (~((1<<0) | (1<<1))); //set 03H's bit[1:0] to 00
1693 }else if (parm->space == FM_SPACE_200K) {
1695 tRegValue &= (~(1<<1)); //clear bit[1]
1696 tRegValue |= (1<<0); //set bit[0]
1700 tRegValue &= (~((1<<0) | (1<<1))); //set 03H's bit[1:0] to 00
1703 if(parm->band == FM_BAND_UE){
1704 tRegValue &= (~((1<<2) | (1<<3)));
1706 fm->max_freq = 1080;
1707 }else if(parm->band == FM_BAND_JAPAN){
1708 tRegValue &= (~(1<<3));
1709 tRegValue |= (1 << 2);
1712 }else if(parm->band == FM_BAND_JAPANW){
1713 tRegValue &= (~(1<<2));
1714 tRegValue |= (1 << 3);
1716 fm->max_freq = 1080;
1718 parm->err = FM_EPARM;
1722 //set space and band
1723 RDAFM_write(client, 0x03, tRegValue);
1727 if(RDAFM_Scan(client, fm->min_freq, fm->max_freq, &(parm->freq), parm->ScanTBL, &(parm->ScanTBLSize), scandir, space)){
1728 parm->err = FM_SUCCESS;
1730 parm->err = FM_SEEK_FAILED;
1737 static int fm_setvol(struct fm *fm, uint32_t vol)
1740 uint16_t tRegValue = 0;
1741 struct i2c_client *client = fm->i2c_client;
1746 FM_DEBUG("fm_setvol:%d\n", vol);
1748 ret = RDAFM_read(client, 0x05, &tRegValue);
1751 tRegValue &= ~(0x000f);
1754 ret = RDAFM_write(client, 0x05, tRegValue);
1761 static int fm_getvol(struct fm *fm, uint32_t *vol)
1765 struct i2c_client *client = fm->i2c_client;
1767 ret = RDAFM_read(client, 0x05, &tRegValue);
1774 *vol = (tRegValue & 0x000F);
1779 static int fm_getrssi(struct fm *fm, uint32_t *rssi)
1783 struct i2c_client *client = fm->i2c_client;
1785 ret = RDAFM_read(client, 0x0B, &tRegValue);
1790 *rssi = (uint32_t)((tRegValue >> 9) & RDAFM_MASK_RSSI);
1792 FM_DEBUG("rssi value:%d\n", *rssi);
1800 static int fm_tune(struct fm *fm, struct fm_tune_parm *parm)
1805 uint16_t bottomOfBand = 875;
1807 struct i2c_client *client = fm->i2c_client;
1809 FM_DEBUG("%s\n", __func__);
1813 parm->err = FM_BADSTATUS;
1817 if (parm->space == FM_SPACE_100K)
1820 val &= (~((1<<0) | (1<<1)));
1822 else if (parm->space == FM_SPACE_200K)
1830 parm->err = FM_EPARM;
1834 if (parm->band == FM_BAND_UE)
1836 val &= (~((1<<2) | (1<<3)));
1839 fm->max_freq = 1080;
1841 else if (parm->band == FM_BAND_JAPAN)
1849 else if (parm->band == FM_BAND_JAPANW) {
1854 fm->max_freq = 1080;
1858 FM_ALERT("band:%d out of range\n", parm->band);
1859 parm->err = FM_EPARM;
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;
1869 FM_DEBUG("fm_tune, freq:%d\n", parm->freq);
1871 //RDAFM_enable_hmute(client);
1873 val = (((parm->freq - bottomOfBand + 5) << 6) | (1 << 4) | (val & 0x0f));
1875 ret = RDAFM_write(client, 0x03, val);
1878 FM_ALERT("fm_tune write freq failed\n");
1879 parm->err = FM_SEEK_FAILED;
1887 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31))
1889 * fm_i2c_attach_adapter
1891 static int fm_i2c_attach_adapter(struct i2c_adapter *adapter)
1895 if (adapter->id == RDAFM_I2C_PORT)
1897 return i2c_probe(adapter, &RDAFM_addr_data, fm_i2c_detect);
1905 * This function is called by i2c_detect
1907 static int fm_i2c_detect(struct i2c_adapter *adapter, int addr, int kind)
1910 struct i2c_client *client = NULL;
1912 /* skip this since MT6516 shall support all the needed functionalities
1913 if (!i2c_check_functionality(adapter, xxx))
1915 FM_DEBUG("i2c_check_functionality failed\n");
1920 /* initial i2c client */
1921 if (!(client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL)))
1923 FM_ALERT("kzalloc failed\n");
1928 client->addr = addr;
1929 client->adapter = adapter;
1930 client->driver = &RDAFM_driver;
1932 strncpy(client->name, "RDA FM RADIO", I2C_NAME_SIZE);
1934 if ((err = fm_init(client)))
1936 FM_ALERT("fm_init ERR:%d\n", err);
1940 if (err = i2c_attach_client(client))
1942 FM_ALERT("i2c_attach_client ERR:%d\n", err);
1953 static int fm_i2c_detach_client(struct i2c_client *client)
1956 struct fm *fm = i2c_get_clientdata(client);
1958 FM_DEBUG("fm_i2c_detach_client\n");
1960 err = i2c_detach_client(client);
1963 dev_err(&client->dev, "fm_i2c_detach_client failed\n");
1973 static int fm_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
1976 FM_DEBUG("fm_i2c_probe\n");
1977 //client->timing = 50;
1978 //client->timing = 200;
1979 if ((err = fm_init(client)))
1981 FM_ALERT("fm_init ERR:%d\n", err);
1991 static int fm_i2c_detect(struct i2c_client *client, int kind, struct i2c_board_info *info)
1993 FM_DEBUG("fm_i2c_detect\n");
1994 strcpy(info->type, RDAFM_DEV);
1998 static int fm_i2c_remove(struct i2c_client *client)
2001 struct fm *fm = i2c_get_clientdata(client);
2003 FM_DEBUG("fm_i2c_remove\n");
2014 int i2c_static_add_device(struct i2c_board_info *info)
2016 struct i2c_adapter *adapter;
2017 struct i2c_client *client;
2020 adapter = i2c_get_adapter(RDAFM_I2C_PORT);
2022 FM_DEBUG("%s: can't get i2c adapter\n", __func__);
2027 client = i2c_new_device(adapter, info);
2029 FM_DEBUG("%s: can't add i2c device at 0x%x\n",
2030 __FUNCTION__, (unsigned int)info->addr);
2035 i2c_put_adapter(adapter);
2043 static int mt_fm_probe(struct platform_device *pdev)
2046 FM_ALERT("mt_fm_probe\n");
2047 err = i2c_static_add_device(&i2c_rdafm);
2049 FM_DEBUG("%s(): add i2c device error, err = %d\n", __func__, err);
2054 err = i2c_add_driver(&RDAFM_driver);
2057 FM_ALERT("i2c err\n");
2063 static int mt_fm_remove(struct platform_device *pdev)
2065 FM_ALERT("mt_fm_remove\n");
2066 i2c_unregister_device(g_fm_struct->i2c_client);
2067 i2c_del_driver(&RDAFM_driver);
2073 static struct platform_driver mt_fm_dev_drv =
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,
2083 .owner = THIS_MODULE,
2087 #if defined(MTK_MT6515)
2088 static struct platform_device mt_fm_device = {
2098 static int __init mt_fm_init(void)
2102 FM_DEBUG("mt_fm_init\n");
2103 #if defined(MTK_MT6515)
2104 err = platform_device_register(&mt_fm_device);
2106 FM_DEBUG("platform_device_register fail\n");
2109 FM_DEBUG("platform_device_register success\n");
2112 err = platform_driver_register(&mt_fm_dev_drv);
2115 FM_DEBUG("platform_driver_register failed\n");
2117 FM_DEBUG("platform_driver_register success\n");
2126 static void __exit mt_fm_exit(void)
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);
2135 module_init(mt_fm_init);
2136 module_exit(mt_fm_exit);
2138 MODULE_LICENSE("GPL");
2139 MODULE_DESCRIPTION("MediaTek FM Driver");
2140 MODULE_AUTHOR("William Chung <William.Chung@MediaTek.com>");