2 * Copyright (C) 2010 MEMSIC, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/jiffies.h>
29 #include <linux/i2c.h>
30 #include <linux/i2c-dev.h>
31 #include <linux/miscdevice.h>
32 #include <linux/mutex.h>
34 #include <linux/device.h>
36 #include <linux/delay.h>
37 #include <linux/sysctl.h>
38 #include <asm/uaccess.h>
39 #include <linux/proc_fs.h>
44 #define DBG(x...) printk(KERN_INFO x)
50 #define FM580X_RETRY_COUNT 3
52 #define FM580X_DEV_NAME "fm580x"
54 static struct i2c_client *this_client;
55 static uint8_t RDA5807P_REG[8];
57 static int fm580x_i2c_rx_data(uint8_t *buf, int len)
60 struct i2c_msg msgs[] = {
62 .addr = this_client->addr,
70 .addr = this_client->addr,
79 for (i = 0; i < FM580X_RETRY_COUNT; i++) {
80 if (i2c_transfer(this_client->adapter, msgs, 2) >= 0) {
86 if (i >= FM580X_RETRY_COUNT) {
87 pr_err("%s: retry over %d\n", __FUNCTION__, FM580X_RETRY_COUNT);
94 static int fm580x_i2c_tx_data(uint8_t *buf, int len)
97 struct i2c_msg msg[] = {
99 .addr = this_client->addr,
106 for (i = 0; i < FM580X_RETRY_COUNT; i++) {
107 if (i2c_transfer(this_client->adapter, msg, 1) >= 0) {
113 if (i >= FM580X_RETRY_COUNT) {
114 pr_err("%s: retry over %d\n", __FUNCTION__, FM580X_RETRY_COUNT);
121 bool fm580x_tuner_init (void)
127 RDA5807P_REG[0] = 0x00;
128 RDA5807P_REG[1] = 0x02;
130 error_ind = fm580x_i2c_tx_data( (uint8_t *)&RDA5807P_REG[0], 2);
133 error_ind = fm580x_i2c_rx_data( (uint8_t *)&RDA5807P_REG[0], 8);
134 gChipID = RDA5807P_REG[6];
135 gChipID = ((gChipID<<8) | RDA5807P_REG[7]);
137 if (gChipID == 0x5804)
140 RDA5807P_REG[i] = RDA5807PE_initialization_reg[i];
141 error_ind = fm580x_i2c_tx_data( (uint8_t *)&RDA5807PE_initialization_reg[0], 2);
143 error_ind = fm580x_i2c_tx_data( (uint8_t *)&RDA5807PE_initialization_reg[0],sizeof(RDA5807PE_initialization_reg));
146 DBG("%s:RDA5807P_REG[0]=0x%x,RDA5807P_REG[1]=0x%x,ID=0x%x\n",__FUNCTION__,RDA5807P_REG[0],RDA5807P_REG[1],gChipID);
152 unsigned short fm580x_tuner_exit (void)
154 RDA5807P_REG[1] &= (~1);
155 fm580x_i2c_tx_data( &(RDA5807P_REG[0]), 2);
158 void fm580x_tuner_mute (unsigned short flag)
161 RDA5807P_REG[0] &= ~(1<<7);
163 RDA5807P_REG[0] |= 1<<7;
164 DBG("fm580x_tuner_mute \n");
165 fm580x_i2c_tx_data( &(RDA5807P_REG[0]), 2);
168 void fm580x_tuner_setStereo(unsigned short flag)
171 RDA5807P_REG[0] &= ~(1<<6);
173 RDA5807P_REG[0] |= 1<<6;
175 fm580x_i2c_tx_data( &(RDA5807P_REG[0]), 2);
179 bool fm580x_tuner_getStereo()
182 if(RDA5807P_REG[0] &(1<<6))
190 uint16_t fm580x_FreqToChan(uint16_t frequency)
192 uint8_t channelSpacing;
193 uint16_t bottomOfBand;
196 if ((RDA5807P_REG[3] & 0x0c) == 0x00)
198 else if ((RDA5807P_REG[3] & 0x0c) == 0x04)
200 else if ((RDA5807P_REG[3] & 0x0c) == 0x08)
203 if ((RDA5807P_REG[3] & 0x03) == 0x00)
205 else if ((RDA5807P_REG[3] & 0x03) == 0x01)
208 channel = (frequency - bottomOfBand) / channelSpacing;
209 DBG("%s: RDA5807P_REG[3]=0x%x,channel=%d,bottomOfBand=%d,channelSpacing=%d\n",__FUNCTION__,RDA5807P_REG[3],channel,bottomOfBand,channelSpacing);
214 void fm580x_set_frequency(unsigned short curFreq)
217 curChan = fm580x_FreqToChan(curFreq/10);
220 //RDA5807P_REG[0] |= 1<<7;
221 RDA5807P_REG[2]=curChan>>2;
222 RDA5807P_REG[3]=(((curChan&0x0003)<<6)|0x10) | (RDA5807P_REG[3]&0x0f); //set tune bit
223 DBG("fm580x_set_frequency %x,%x,%x \n",RDA5807P_REG[0],RDA5807P_REG[2],RDA5807P_REG[3]);
224 fm580x_i2c_tx_data( &(RDA5807P_REG[0]), 4);
225 mdelay(10); //Delay five ms
226 DBG("%s:curchan=0x%x\n",__FUNCTION__,curChan);
229 bool fm580x_tuner_CheckStation()
232 uint8_t RDA5807P_reg_data[4]={0};
234 //fm580x_i2c_rx_data(&(RDA5807P_reg_data[0]), 4);
241 fm580x_i2c_rx_data(&(RDA5807P_reg_data[0]), 4);
242 }while((RDA5807P_reg_data[3]&0x80)==0);
243 DBG("fm580x_tuner_CheckStation %x ,%x\n",RDA5807P_reg_data[2],RDA5807P_reg_data[3]);
244 return (RDA5807P_reg_data[2]&(1<<0));
247 static int fm580x_init_device(struct i2c_client *client)
254 static ssize_t fm580x_proc_write(struct file *file, const char __user *buffer,
255 unsigned long count, void *data)
261 rc = get_user(c, buffer);
265 freq = 10000*(c - '0');
268 fm580x_tuner_setStereo(1);
269 fm580x_set_frequency(freq);
270 fm580x_tuner_CheckStation();
272 printk("%s:freq=%d\n",__FUNCTION__,freq);
277 static const struct file_operations fm580x_proc_fops = {
278 .owner = THIS_MODULE,
279 .write = fm580x_proc_write,
282 static int fm580x_open(struct inode *inode, struct file *file)
284 return nonseekable_open(inode, file);
287 static int fm580x_release(struct inode *inode, struct file *file)
292 static int fm580x_ioctl(struct inode *inode, struct file *file,
293 unsigned int cmd, unsigned long arg)
301 DBG("set fm580x area \n");
304 case FM_MUTE://mute fm
305 DBG("set fm580x mute \n");
306 fm580x_tuner_mute(arg);
310 DBG("enable fm580x chip \n");
312 //rk1000_codec_all_line_input_open(&rk1000_codec_dai,&codecdata1,&codecdata2);
316 DBG("disable fm580x chip \n");
317 //rk1000_codec_all_line_input_close(&rk1000_codec_dai,codecdata1,codecdata2);
322 DBG("set fm580x stereo \n");
323 fm580x_tuner_setStereo(arg);
327 DBG("get fm580x stereo \n");
328 status = fm580x_tuner_getStereo();
329 ret = put_user(status,(int *)arg);
331 DBG("put_user err!\n");
336 ret=copy_from_user(&freq,(void __user *) arg, sizeof(int));
338 DBG("put_user err!\n");
341 fm580x_set_frequency(freq);
344 case FM_STATION_ISAVAILABLE:
345 status = fm580x_tuner_CheckStation();
346 ret = put_user(status,(int *)arg);
348 DBG("put_user err!\n");
350 DBG(" fm580x station's state is %d \n",status);
354 DBG("set fm580x tr fun \n");
359 DBG("stop fm580x tr fun \n");
365 //E("unknown ioctl cmd!\n");
373 static ssize_t fm580x_show(struct device *dev, struct device_attribute *attr, char *buf)
376 sprintf(buf, "fm580x");
377 ret = strlen(buf) + 1;
378 printk("%s:ret=%d\n",__FUNCTION__,ret);
382 static DEVICE_ATTR(fm580x, S_IRUGO, fm580x_show, NULL);
384 static struct file_operations fm580x_fops = {
385 .owner = THIS_MODULE,
387 .release = fm580x_release,
388 .ioctl = fm580x_ioctl,
391 static struct miscdevice fm580x_device = {
392 .minor = MISC_DYNAMIC_MINOR,
393 .name = FM580X_DEV_NAME,
394 .fops = &fm580x_fops,
397 static int fm580x_probe(struct i2c_client *client, const struct i2c_device_id *id)
399 unsigned char data[16] = {0};
401 struct proc_dir_entry *fm580x_proc_entry;
403 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
405 pr_err("%s: functionality check failed\n", __FUNCTION__);
410 this_client = client;
412 res = misc_register(&fm580x_device);
414 pr_err("%s: fm580x_device register failed\n", __FUNCTION__);
418 res = device_create_file(&client->dev, &dev_attr_fm580x);
420 pr_err("%s: device_create_file failed\n", __FUNCTION__);
424 //fm580x_tuner_init();
426 fm580x_proc_entry = proc_create("driver/fm580x", 0777, NULL, &fm580x_proc_fops);
428 printk("%s:line=%d\n",__FUNCTION__,__LINE__);
433 misc_deregister(&fm580x_device);
438 static int fm580x_remove(struct i2c_client *client)
440 device_remove_file(&client->dev, &dev_attr_fm580x);
441 misc_deregister(&fm580x_device);
445 static const struct i2c_device_id fm580x_id[] = {
446 { FM580X_I2C_NAME, 0 },
450 static struct i2c_driver fm580x_driver = {
451 .probe = fm580x_probe,
452 .remove = fm580x_remove,
453 .id_table = fm580x_id,
455 .owner = THIS_MODULE,
456 .name = FM580X_I2C_NAME,
461 static int __init fm580x_init(void)
463 pr_info("fm580x driver: init\n");
464 return i2c_add_driver(&fm580x_driver);
467 static void __exit fm580x_exit(void)
469 pr_info("fm580x driver: exit\n");
470 i2c_del_driver(&fm580x_driver);
473 module_init(fm580x_init);
474 module_exit(fm580x_exit);
476 MODULE_AUTHOR("luo wei<lw@rock-chips.com>");
477 MODULE_DESCRIPTION("FM rda580x Driver");
478 MODULE_LICENSE("GPL");