1 #include <linux/module.h>
2 #include <linux/init.h>
4 #include <linux/device.h>
5 #include <linux/delay.h>
7 #include <linux/mfd/core.h>
8 #include <linux/mfd/rk610_core.h>
10 #include <mach/iomux.h>
11 #include <linux/err.h>
12 #include <linux/slab.h>
14 #if defined(CONFIG_DEBUG_FS)
16 #include <linux/debugfs.h>
17 #include <linux/seq_file.h>
18 #include <linux/uaccess.h>
28 #define DBG(x...) printk(KERN_INFO x)
33 static struct i2c_client *rk610_control_client = NULL;
36 static struct mfd_cell rk610_devs[] = {
43 int rk610_control_send_byte(const char reg, const char data)
47 DBG("reg = 0x%02x, val=0x%02x\n", reg ,data);
49 if(rk610_control_client == NULL)
51 //i2c_master_reg8_send
52 ret = i2c_master_reg8_send(rk610_control_client, reg, &data, 1, 100*1000);
59 #ifdef CONFIG_SND_SOC_RK610
60 static unsigned int current_pll_value = 0;
61 int rk610_codec_pll_set(unsigned int rate)
67 if(current_pll_value == rate)
70 // Input clock is 12MHz.
71 if(rate == 11289600) {
72 // For 11.2896MHz, N = 2 M= 75 F = 0.264(0x43958) NO = 8
79 else if(rate == 12288000) {
80 // For 12.2888MHz, N = 2 M= 75 F = 0.92(0xEB851) NO = 8
88 printk(KERN_ERR "[%s] not support such frequency\n", __FUNCTION__);
92 //Enable codec pll fractional number and power down.
94 rk610_control_send_byte(RK610_CONTROL_REG_C_PLL_CON5, data);
98 rk610_control_send_byte(RK610_CONTROL_REG_C_PLL_CON0, data);
101 rk610_control_send_byte(RK610_CONTROL_REG_C_PLL_CON1, data);
104 rk610_control_send_byte(RK610_CONTROL_REG_C_PLL_CON2, data);
105 data = (F >> 8) & 0xFF;
106 rk610_control_send_byte(RK610_CONTROL_REG_C_PLL_CON3, data);
107 data = (F >> 16) & 0xFF;
108 rk610_control_send_byte(RK610_CONTROL_REG_C_PLL_CON4, data);
110 // i2s mclk = codec_pll/5;
111 i2c_master_reg8_recv(rk610_control_client, RK610_CONTROL_REG_CLOCK_CON1, &data, 1, 100*1000);
112 data &= ~CLOCK_CON1_I2S_DVIDER_MASK;
114 rk610_control_send_byte(RK610_CONTROL_REG_CLOCK_CON1, data);
116 // Power up codec pll.
117 data |= C_PLL_POWER_ON;
118 rk610_control_send_byte(RK610_CONTROL_REG_C_PLL_CON5, data);
120 current_pll_value = rate;
121 DBG("[%s] rate %u\n", __FUNCTION__, rate);
126 void rk610_control_init_codec(void)
128 struct i2c_client *client = rk610_control_client;
132 if(rk610_control_client == NULL)
134 DBG("[%s] start\n", __FUNCTION__);
136 //gpio_set_value(RK610_RESET_PIN, GPIO_LOW); //reset rk601
138 //gpio_set_value(RK610_RESET_PIN, GPIO_HIGH);
141 // Set i2c glitch timeout.
143 ret = i2c_master_reg8_send(client, RK610_CONTROL_REG_I2C_CON, &data, 1, 20*1000);
145 // rk610_codec_pll_set(11289600);
147 //use internal codec, enable DAC ADC LRCK output.
148 // i2c_master_reg8_recv(client, RK610_CONTROL_REG_CODEC_CON, &data, 1, 100*1000);
149 // data = CODEC_CON_BIT_DAC_LRCL_OUTPUT_DISABLE | CODEC_CON_BIT_ADC_LRCK_OUTPUT_DISABLE;
150 // data = CODEC_CON_BIT_ADC_LRCK_OUTPUT_DISABLE;
152 rk610_control_send_byte(RK610_CONTROL_REG_CODEC_CON, data);
154 // Select internal i2s clock from codec_pll.
155 i2c_master_reg8_recv(rk610_control_client, RK610_CONTROL_REG_CLOCK_CON1, &data, 1, 100*1000);
156 // data |= CLOCK_CON1_I2S_CLK_CODEC_PLL;
158 rk610_control_send_byte(RK610_CONTROL_REG_CLOCK_CON1, data);
160 i2c_master_reg8_recv(client, RK610_CONTROL_REG_CODEC_CON, &data, 1, 100*1000);
161 DBG("[%s] RK610_CONTROL_REG_CODEC_CON is %x\n", __FUNCTION__, data);
163 i2c_master_reg8_recv(client, RK610_CONTROL_REG_CLOCK_CON1, &data, 1, 100*1000);
164 DBG("[%s] RK610_CONTROL_REG_CLOCK_CON1 is %x\n", __FUNCTION__, data);
168 static int rk610_read_p0_reg(struct i2c_client *client, char reg, char *val)
170 return i2c_master_reg8_recv(client, reg, val, 1, 100*1000) > 0? 0: -EINVAL;
173 static int rk610_write_p0_reg(struct i2c_client *client, char reg, char *val)
175 return i2c_master_reg8_send(client, reg, val, 1, 100*1000) > 0? 0: -EINVAL;
179 #if defined(CONFIG_DEBUG_FS)
180 static int rk610_reg_show(struct seq_file *s, void *v)
184 struct rk610_core_info *core_info = s->private;
187 dev_err(core_info->dev,"no mfd rk610!\n");
191 for(reg=C_PLL_CON0;reg<= I2C_CON;reg++)
193 rk610_read_p0_reg(core_info->client, reg, &val);
195 seq_printf(s,"\n0x%02x:",reg);
196 seq_printf(s," %02x",val);
203 static ssize_t rk610_reg_write (struct file *file, const char __user *buf, size_t count, loff_t *ppos)
205 struct rk610_core_info *core_info = file->f_path.dentry->d_inode->i_private;
209 if (copy_from_user(kbuf, buf, count))
211 sscanf(kbuf, "%x%x", ®,&val);
212 rk610_write_p0_reg(core_info->client, reg, (u8*)&val);
216 static int rk610_reg_open(struct inode *inode, struct file *file)
218 struct rk610_core_info *core_info = inode->i_private;
219 return single_open(file,rk610_reg_show,core_info);
222 static const struct file_operations rk610_reg_fops = {
223 .owner = THIS_MODULE,
224 .open = rk610_reg_open,
226 .write = rk610_reg_write,
228 .release = single_release,
232 static int rk610_control_probe(struct i2c_client *client,
233 const struct i2c_device_id *id)
237 struct rk610_core_info *core_info = NULL;
238 struct rk610_ctl_platform_data *pdata = client->dev.platform_data;
239 DBG("[%s] start\n", __FUNCTION__);
240 core_info = kmalloc(sizeof(struct rk610_core_info), GFP_KERNEL);
243 dev_err(&client->dev, ">> rk610 core inf kmalloc fail!");
246 memset(core_info, 0, sizeof(struct rk610_core_info));
247 core_info->pdata = pdata;
248 #if defined(CONFIG_SND_RK29_SOC_I2S_8CH)
249 iis_clk = clk_get_sys("rk29_i2s.0", "i2s");
250 #elif defined(CONFIG_SND_RK29_SOC_I2S_2CH)
251 iis_clk = clk_get_sys("rk29_i2s.1", "i2s");
253 iis_clk = clk_get_sys("rk29_i2s.2", "i2s");
255 if (IS_ERR(iis_clk)) {
256 printk("failed to get i2s clk\n");
257 ret = PTR_ERR(iis_clk);
259 DBG("got i2s clk ok!\n");
261 clk_set_rate(iis_clk, 11289600);
262 #if defined(CONFIG_ARCH_RK29)
263 rk29_mux_api_set(GPIO2D0_I2S0CLK_MIIRXCLKIN_NAME, GPIO2H_I2S0_CLK);
264 #elif defined(CONFIG_ARCH_RK3066B)||defined(CONFIG_ARCH_RK3188)
265 iomux_set(I2S0_MCLK);
266 #elif defined(CONFIG_ARCH_RK30)
267 rk30_mux_api_set(GPIO0B0_I2S8CHCLK_NAME, GPIO0B_I2S_8CH_CLK);
272 rk610_control_client = client;
273 if(core_info->pdata->rk610_power_on_init)
274 core_info->pdata->rk610_power_on_init();
275 core_info->client = client;
276 core_info->dev = &client->dev;
277 i2c_set_clientdata(client,core_info);
278 ret = mfd_add_devices(&client->dev, -1,
279 rk610_devs, ARRAY_SIZE(rk610_devs),
282 #if defined(CONFIG_DEBUG_FS)
283 core_info->debugfs_dir = debugfs_create_dir("rk610", NULL);
284 if (IS_ERR(core_info->debugfs_dir))
286 dev_err(&client->dev,"failed to create debugfs dir for rk610!\n");
289 debugfs_create_file("core", S_IRUSR,core_info->debugfs_dir,core_info,&rk610_reg_fops);
295 static int rk610_control_remove(struct i2c_client *client)
300 static const struct i2c_device_id rk610_control_id[] = {
304 MODULE_DEVICE_TABLE(i2c, rk610_control_id);
306 static struct i2c_driver rk610_control_driver = {
310 .probe = rk610_control_probe,
311 .remove = rk610_control_remove,
312 .id_table = rk610_control_id,
315 static int __init rk610_control_init(void)
317 return i2c_add_driver(&rk610_control_driver);
320 static void __exit rk610_control_exit(void)
322 i2c_del_driver(&rk610_control_driver);
325 subsys_initcall_sync(rk610_control_init);
326 module_exit(rk610_control_exit);
329 MODULE_DESCRIPTION("RK610 control driver");
330 MODULE_AUTHOR("Rock-chips, <www.rock-chips.com>");
331 MODULE_LICENSE("GPL");