2 #include <linux/kernel.h>
3 #include <linux/module.h>
4 #include <linux/delay.h>
5 #include <linux/mfd/core.h>
6 #include <linux/slab.h>
7 #include <linux/mfd/rk616.h>
9 #include <mach/iomux.h>
10 #include <linux/err.h>
11 #include <linux/uaccess.h>
12 #if defined(CONFIG_DEBUG_FS)
14 #include <linux/debugfs.h>
15 #include <linux/seq_file.h>
19 #define MHZ (1000*1000)
22 static struct mfd_cell rk616_devs[] = {
28 .name = "rk616-codec",
41 static int rk616_i2c_read_reg(struct mfd_rk616 *rk616, u16 reg,u32 *pval)
43 struct i2c_client * client = rk616->client;
44 struct i2c_adapter *adap = client->adapter;
45 struct i2c_msg msgs[2];
49 memcpy(reg_buf, ®, 2);
51 msgs[0].addr = client->addr;
52 msgs[0].flags = client->flags;
54 msgs[0].buf = reg_buf;
55 msgs[0].scl_rate = rk616->pdata->scl_rate;
56 msgs[0].udelay = client->udelay;
58 msgs[1].addr = client->addr;
59 msgs[1].flags = client->flags | I2C_M_RD;
61 msgs[1].buf = (char *)pval;
62 msgs[1].scl_rate = rk616->pdata->scl_rate;
63 msgs[1].udelay = client->udelay;
65 ret = i2c_transfer(adap, msgs, 2);
68 return (ret == 2)? 4 : ret;
72 static int rk616_i2c_write_reg(struct mfd_rk616 *rk616, u16 reg,u32 *pval)
74 struct i2c_client *client = rk616->client;
75 struct i2c_adapter *adap = client->adapter;
78 char *tx_buf = (char *)kmalloc(6, GFP_KERNEL);
82 memcpy(tx_buf, ®, 2);
83 memcpy(tx_buf+2, (char *)pval, 4);
85 msg.addr = client->addr;
86 msg.flags = client->flags;
88 msg.buf = (char *)tx_buf;
89 msg.scl_rate = rk616->pdata->scl_rate;
90 msg.udelay = client->udelay;
92 ret = i2c_transfer(adap, &msg, 1);
96 return (ret == 1) ? 4 : ret;
100 #if defined(CONFIG_DEBUG_FS)
101 static int rk616_reg_show(struct seq_file *s, void *v)
105 struct mfd_rk616 *rk616 = s->private;
108 dev_err(rk616->dev,"no mfd rk616!\n");
112 for(i=0;i<= CRU_CFGMISC_CON;i+=4)
114 rk616->read_dev(rk616,i,&val);
116 seq_printf(s,"\n0x%04x:",i);
117 seq_printf(s," %08x",val);
124 static ssize_t rk616_reg_write (struct file *file, const char __user *buf, size_t count, loff_t *ppos)
126 struct mfd_rk616 *rk616 = file->f_path.dentry->d_inode->i_private;
131 if (copy_from_user(kbuf, buf, count))
133 sscanf(kbuf, "%x%x", ®,&val);
134 dev_dbg(rk616->dev,"%s:reg:0x%04x val:0x%08x\n",__func__,reg,val);
135 rk616->write_dev(rk616,reg,&val);
139 static int rk616_reg_open(struct inode *inode, struct file *file)
141 struct mfd_rk616 *rk616 = inode->i_private;
142 return single_open(file,rk616_reg_show,rk616);
145 static const struct file_operations rk616_reg_fops = {
146 .owner = THIS_MODULE,
147 .open = rk616_reg_open,
149 .write = rk616_reg_write,
151 .release = single_release,
156 static u32 rk616_clk_gcd(u32 numerator, u32 denominator)
160 if (!numerator || !denominator)
163 if (numerator > denominator) {
181 static int rk616_pll_par_calc(u32 fin_hz,u32 fout_hz,u32 *refdiv, u32 *fbdiv,
182 u32 *postdiv1, u32 *postdiv2, u32 *frac)
184 // FIXME set postdiv1/2 always 1
188 if(!fin_hz || !fout_hz)
191 if(fin_hz / MHZ * MHZ == fin_hz && fout_hz /MHZ * MHZ == fout_hz)
195 gcd = rk616_clk_gcd(fin_hz, fout_hz);
196 *refdiv = fin_hz / gcd;
197 *fbdiv = fout_hz / gcd;
207 gcd = rk616_clk_gcd(fin_hz / MHZ, fout_hz / MHZ);
208 *refdiv = fin_hz / MHZ / gcd;
209 *fbdiv = fout_hz / MHZ / gcd;
215 f_frac = (fout_hz % MHZ);
217 do_div(fin_64, (u64)*refdiv);
218 frac_64 = (u64)f_frac << 24;
219 do_div(frac_64, fin_64);
220 *frac = (u32) frac_64;
221 printk(KERN_INFO "frac_64=%llx, frac=%u\n", frac_64, *frac);
223 printk(KERN_INFO "fin=%u,fout=%u,gcd=%u,refdiv=%u,fbdiv=%u,postdiv1=%u,postdiv2=%u,frac=%u\n",
224 fin_hz, fout_hz, gcd, *refdiv, *fbdiv, *postdiv1, *postdiv2, *frac);
229 static int rk616_pll_wait_lock(struct mfd_rk616 *rk616,int id)
246 ret = rk616->read_dev(rk616,CRU_PLL0_CON1 + offset,&val);
249 dev_info(rk616->dev,"PLL%d locked\n",id);
257 printk(KERN_ALERT "rk616 wait pll bit time out!\n");
264 int rk616_pll_set_rate(struct mfd_rk616 *rk616,int id,u32 cfg_val,u32 frac)
269 u16 con0 = cfg_val & 0xffff;
270 u16 con1 = (cfg_val >> 16)&0xffff;
271 u32 fbdiv = con0 & 0xfff;
272 u32 postdiv1 = (con0 >> 12)&0x7;
273 u32 refdiv = con1 & 0x3f;
274 u32 postdiv2 = (con1 >> 6) & 0x7;
288 val = PLL0_PWR_DN | (PLL0_PWR_DN << 16);
289 ret = rk616->write_dev(rk616,CRU_PLL0_CON1 + offset,&val);
291 val = PLL0_POSTDIV1(postdiv1) | PLL0_FBDIV(fbdiv) | PLL0_POSTDIV1_MASK |
292 PLL0_FBDIV_MASK | (PLL0_BYPASS << 16);
293 ret = rk616->write_dev(rk616,CRU_PLL0_CON0 + offset,&val);
295 val = PLL0_DIV_MODE(mode) | PLL0_POSTDIV2(postdiv2) | PLL0_REFDIV(refdiv) |
296 (PLL0_DIV_MODE_MASK) | PLL0_POSTDIV2_MASK | PLL0_REFDIV_MASK;
297 ret = rk616->write_dev(rk616,CRU_PLL0_CON1 + offset,&val);
299 val = PLL0_FRAC(frac);
300 ret = rk616->write_dev(rk616,CRU_PLL0_CON2 + offset,&val);
302 val = (PLL0_PWR_DN << 16);
303 ret = rk616->write_dev(rk616,CRU_PLL0_CON1 + offset,&val);
304 rk616_pll_wait_lock(rk616,id);
309 /***********************************
310 default clk patch settiing:
312 LCD_DCLK0--->PLL0--->Dither--->LVDS/MIPI
313 LCD_DCLK1--->PLL1--->HDMI
314 ************************************/
316 static int rk616_clk_common_init(struct mfd_rk616 *rk616)
321 val = PLL1_CLK_SEL(LCD1_DCLK) | PLL0_CLK_SEL(LCD0_DCLK) | LCD1_CLK_DIV(0) |
322 LCD0_CLK_DIV(0) | PLL1_CLK_SEL_MASK | PLL0_CLK_SEL_MASK |
323 LCD1_CLK_DIV_MASK | LCD0_CLK_DIV_MASK; //pll1 clk from lcdc1_dclk,pll0 clk from lcdc0_dclk,mux_lcdx = lcdx_clk
324 ret = rk616->write_dev(rk616,CRU_CLKSEL0_CON,&val);
326 val = SCLK_SEL(SCLK_SEL_PLL1) | CODEC_MCLK_SEL(CODEC_MCLK_SEL_12M) |
327 CODEC_MCLK_SEL_MASK | SCLK_SEL_MASK; //codec mclk from clkin
328 ret = rk616->write_dev(rk616,CRU_CLKSEL1_CON,&val);
330 val = 0; //codec mck = clkin
331 ret = rk616->write_dev(rk616,CRU_CODEC_DIV,&val);
333 val = (PLL0_BYPASS) | (PLL0_BYPASS << 16); //bypass pll0
334 ret = rk616->write_dev(rk616,CRU_PLL0_CON0,&val);
336 val = (PLL1_BYPASS) | (PLL1_BYPASS << 16);
337 ret = rk616->write_dev(rk616,CRU_PLL1_CON0,&val);
343 static int rk616_i2c_probe(struct i2c_client *client,const struct i2c_device_id *id)
346 struct mfd_rk616 *rk616 = NULL;
349 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
351 dev_err(&client->dev, "Must have I2C_FUNC_I2C.\n");
354 rk616 = kzalloc(sizeof(struct mfd_rk616), GFP_KERNEL);
357 printk(KERN_ALERT "alloc for struct rk616 fail\n");
361 rk616->dev = &client->dev;
362 rk616->pdata = client->dev.platform_data;
363 rk616->client = client;
364 i2c_set_clientdata(client, rk616);
365 dev_set_drvdata(rk616->dev,rk616);
367 if(rk616->pdata->power_init)
368 rk616->pdata->power_init();
370 #if defined(CONFIG_SND_RK29_SOC_I2S_8CH)
371 iis_clk = clk_get_sys("rk29_i2s.0", "i2s");
372 #elif defined(CONFIG_SND_RK29_SOC_I2S_2CH)
373 iis_clk = clk_get_sys("rk29_i2s.1", "i2s");
375 iis_clk = clk_get_sys("rk29_i2s.2", "i2s");
379 dev_err(&client->dev,"failed to get i2s clk\n");
380 ret = PTR_ERR(iis_clk);
384 #if defined(CONFIG_ARCH_RK29)
385 rk29_mux_api_set(GPIO2D0_I2S0CLK_MIIRXCLKIN_NAME, GPIO2H_I2S0_CLK);
390 clk_set_rate(iis_clk, 11289600);
393 rk616->read_dev = rk616_i2c_read_reg;
394 rk616->write_dev = rk616_i2c_write_reg;
396 #if defined(CONFIG_DEBUG_FS)
397 rk616->debugfs_dir = debugfs_create_dir("rk616", NULL);
398 if (IS_ERR(rk616->debugfs_dir))
400 dev_err(rk616->dev,"failed to create debugfs dir for rk616!\n");
403 debugfs_create_file("core", S_IRUSR,rk616->debugfs_dir,rk616,&rk616_reg_fops);
405 rk616_clk_common_init(rk616);
406 ret = mfd_add_devices(rk616->dev, -1,
407 rk616_devs, ARRAY_SIZE(rk616_devs),
408 NULL, rk616->irq_base);
410 dev_info(&client->dev,"rk616 core probe success!\n");
414 static int __devexit rk616_i2c_remove(struct i2c_client *client)
419 static const struct i2c_device_id id_table[] = {
424 static struct i2c_driver rk616_i2c_driver = {
427 .owner = THIS_MODULE,
429 .probe = &rk616_i2c_probe,
430 .remove = &rk616_i2c_remove,
431 .id_table = id_table,
435 static int __init rk616_module_init(void)
437 return i2c_add_driver(&rk616_i2c_driver);
440 static void __exit rk616_module_exit(void)
442 i2c_del_driver(&rk616_i2c_driver);
445 subsys_initcall_sync(rk616_module_init);
446 module_exit(rk616_module_exit);