2 * rk1000.c -- RK1000 ALSA SoC audio driver
4 * Copyright (C) 2009 rockchip lhh
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/slab.h>
24 #include <sound/core.h>
25 #include <sound/jack.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/initval.h>
30 #include <sound/tlv.h>
31 #include <trace/events/asoc.h>
32 #include <mach/gpio.h>
33 #include <mach/iomux.h>
35 #include "rk1000_codec.h"
36 #define RK1000_CODEC_PROC
37 #ifdef RK1000_CODEC_PROC
38 #include <linux/proc_fs.h>
39 #include <linux/seq_file.h>
40 #include <linux/vmalloc.h>
41 char debug_write_read = 0;
47 #define DBG(x...) printk(KERN_INFO x)
52 #define err(format, arg...) \
53 printk(KERN_ERR AUDIO_NAME ": " format "\n" , ## arg)
54 #define info(format, arg...) \
55 printk(KERN_INFO AUDIO_NAME ": " format "\n" , ## arg)
57 #define OUT_CAPLESS (0) //ÊÇ·ñΪÎÞµçÈÝÊä³ö£¬1:ÎÞµçÈÝÊä³ö£¬0:ÓеçÈÝÊä³ö
59 static struct snd_soc_codec *rk1000_codec_codec;
61 //static u32 gVolReg = 0x0f; ///0x0f; //ÓÃÓڼǼÒôÁ¿¼Ä´æÆ÷
62 //static u32 gCodecVol = 0x0f;
63 static u8 gR0AReg = 0; //ÓÃÓڼǼR0A¼Ä´æÆ÷µÄÖµ£¬ÓÃÓڸıä²ÉÑùÂÊǰͨ¹ýR0AÍ£Ö¹clk
64 static u8 gR0BReg = 0; //ÓÃÓڼǼR0B¼Ä´æÆ÷µÄÖµ£¬ÓÃÓڸıä²ÉÑùÂÊǰͨ¹ýR0BÍ£Ö¹interplateºÍdecimation
65 static u8 gR1314Reg = 0; //ÓÃÓڼǼR13,R14¼Ä´æÆ÷µÄÖµ£¬ÓÃÓÚFMÒôÁ¿Îª0ʱ
68 * rk1000 register cache
69 * We can't read the RK1000 register space when we
70 * are using 2 wire for device control, so we cache them instead.
72 static const u16 rk1000_codec_reg[] = {
73 0x0005, 0x0004, 0x00fd, 0x00f3, /* 0 */
74 0x0003, 0x0000, 0x0000, 0x0000, /* 4 */
75 0x0000, 0x0005, 0x0000, 0x0000, /* 8 */
76 0x0097, 0x0097, 0x0097, 0x0097, /* 0x0a */
77 0x0097, 0x0097, 0x00cc, 0x0000, /* 0x10 */
78 0x0000, 0x00f1, 0x0090, 0x00ff, /* 0x14 */
79 0x00ff, 0x00ff, 0x009c, 0x0000, /* 0x18 */
80 0x0000, 0x00ff, 0x00ff, 0x00ff, /* 0x1a */
83 /* codec private data */
84 struct rk1000_codec_priv {
85 enum snd_soc_control_type control_type;
89 struct snd_soc_codec codec;
90 struct snd_pcm_hw_constraint_list *sysclk_constraints;
91 u16 reg_cache[RK1000_CODEC_NUM_REG];
95 * read rk1000 register cache
97 static inline unsigned int rk1000_codec_read_reg_cache(struct snd_soc_codec *codec,
100 u16 *cache = codec->reg_cache;
101 if (reg > RK1000_CACHE_REGNUM)
106 static unsigned int rk1000_codec_read(struct snd_soc_codec *codec, unsigned int r)
108 struct i2c_msg xfer[1];
111 struct i2c_client *client = codec->control_data;
114 xfer[0].addr = (client->addr& 0x60)|(reg);
115 xfer[0].flags = I2C_M_RD;
118 xfer[0].scl_rate = 100*1000;
119 ret = i2c_transfer(client->adapter, xfer, 1);
121 dev_err(&client->dev, "i2c_transfer() returned %d\n", ret);
129 * write rk1000 register cache
131 static inline void rk1000_codec_write_reg_cache(struct snd_soc_codec *codec,
132 unsigned int reg, unsigned int value)
134 u16 *cache = codec->reg_cache;
135 if (reg > RK1000_CACHE_REGNUM)
140 static int rk1000_codec_write(struct snd_soc_codec *codec, unsigned int reg,
144 struct i2c_client *i2c;
145 DBG("Enter-%s::reg=0x%02X, value=0x%02X\n",__FUNCTION__, reg, value);
146 data[0] = value & 0x00ff;
147 rk1000_codec_write_reg_cache (codec, reg, value);
148 i2c = (struct i2c_client *)codec->control_data;
149 i2c->addr = (i2c->addr & 0x60)|reg;
150 if (codec->hw_write(codec->control_data, data, 1) == 1){
151 // DBG("================%s Run OK====%d============\n",__FUNCTION__,__LINE__);
154 DBG("================%s Run EIO=======%d=========\n",__FUNCTION__,__LINE__);
159 static const struct snd_kcontrol_new rk1000_codec_snd_controls[] = {
161 SOC_DOUBLE_R("Capture Volume", ACCELCODEC_R0C, ACCELCODEC_R0D, 0, 15, 0),
162 SOC_DOUBLE_R("Capture Switch", ACCELCODEC_R0C, ACCELCODEC_R0D, 7, 1, 1),
164 SOC_DOUBLE_R("PCM Volume", ACCELCODEC_R0D, ACCELCODEC_R0E, 0, 7, 0),
166 //SOC_SINGLE("Left ADC Capture Volume", ACCELCODEC_R17, 0, 63, 0),
167 //SOC_SINGLE("Right ADC Capture Volume", ACCELCODEC_R18, 0, 63, 0),
174 static const struct snd_kcontrol_new rk1000_codec_left_mixer_controls[] = {
175 SOC_DAPM_SINGLE("Playback Switch", ACCELCODEC_R15, 6, 1, 0),
176 SOC_DAPM_SINGLE("Left Bypass Switch", ACCELCODEC_R15, 2, 1, 0),
181 static const struct snd_kcontrol_new rk1000_codec_right_mixer_controls[] = {
182 SOC_DAPM_SINGLE("Playback Switch", ACCELCODEC_R15, 7, 1, 0),
183 SOC_DAPM_SINGLE("Left Bypass Switch", ACCELCODEC_R15, 3, 1, 0),
188 static const struct snd_soc_dapm_widget rk1000_codec_dapm_widgets[] = {
189 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
190 &rk1000_codec_left_mixer_controls[0],
191 ARRAY_SIZE(rk1000_codec_left_mixer_controls)),
192 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
193 &rk1000_codec_right_mixer_controls[0],
194 ARRAY_SIZE(rk1000_codec_right_mixer_controls)),
196 //SND_SOC_DAPM_PGA("Right Out 1", ACCELCODEC_R1E, 0, 0, NULL, 0),
197 //SND_SOC_DAPM_PGA("Left Out 1", ACCELCODEC_R1E, 1, 0, NULL, 0),
198 //SND_SOC_DAPM_DAC("Right DAC", "Right Playback", ACCELCODEC_R1F, 1, 0),
199 //SND_SOC_DAPM_DAC("Left DAC", "Left Playback", ACCELCODEC_R1F, 2, 0),
201 SND_SOC_DAPM_ADC("ADC", "Capture", ACCELCODEC_R1D, 6, 1),
202 SND_SOC_DAPM_ADC("ADC BUFF", "Capture BUFF", ACCELCODEC_R1D, 2, 0),
205 SND_SOC_DAPM_OUTPUT("LOUT1"),
206 SND_SOC_DAPM_OUTPUT("ROUT1"),
208 SND_SOC_DAPM_INPUT("LINPUT1"),
209 SND_SOC_DAPM_INPUT("RINPUT1"),
212 static const struct snd_soc_dapm_route audio_map[] = {
214 {"Left Mixer", "Playback Switch", "Left DAC"},
215 {"Left Mixer", "Left Bypass Switch", "Left Line Mux"},
216 {"Right Mixer", "Playback Switch", "Right DAC"},
217 {"Right Mixer", "Right Bypass Switch", "Right Line Mux"},
220 {"Left Out 1", NULL, "Left Mixer"},
221 {"LOUT1", NULL, "Left Out 1"},
225 {"Right Out 1", NULL, "Right Mixer"},
226 {"ROUT1", NULL, "Right Out 1"},
229 {"Left Line Mux", "Line 1", "LINPUT1"},
230 {"Left Line Mux", "PGA", "Left PGA Mux"},
231 {"Left Line Mux", "Differential", "Differential Mux"},
234 {"Right Line Mux", "Line 1", "RINPUT1"},
235 {"Right Line Mux", "PGA", "Right PGA Mux"},
236 {"Right Line Mux", "Differential", "Differential Mux"},
239 {"Left PGA Mux", "Line 1", "LINPUT1"},
240 {"Left PGA Mux", "Line 2", "LINPUT2"},
241 {"Left PGA Mux", "Line 3", "LINPUT3"},
242 {"Left PGA Mux", "Differential", "Differential Mux"},
245 {"Right PGA Mux", "Line 1", "RINPUT1"},
246 {"Right PGA Mux", "Differential", "Differential Mux"},
248 /* Differential Mux */
249 {"Differential Mux", "Line 1", "LINPUT1"},
250 {"Differential Mux", "Line 1", "RINPUT1"},
253 {"Left ADC Mux", "Stereo", "Left PGA Mux"},
254 {"Left ADC Mux", "Mono (Left)", "Left PGA Mux"},
255 {"Left ADC Mux", "Digital Mono", "Left PGA Mux"},
258 {"Right ADC Mux", "Stereo", "Right PGA Mux"},
259 {"Right ADC Mux", "Mono (Right)", "Right PGA Mux"},
260 {"Right ADC Mux", "Digital Mono", "Right PGA Mux"},
263 {"Left ADC", NULL, "Left ADC Mux"},
264 {"Right ADC", NULL, "Right ADC Mux"},
279 /* codec hifi mclk clock divider coefficients */
280 static const struct _coeff_div coeff_div[] = {
282 {12288000, 8000, 1536, 0x6, 0x0,ASC_BCLKDIV_16},
283 {11289600, 8000, 1408, 0x16, 0x0,ASC_BCLKDIV_16},
284 {18432000, 8000, 2304, 0x7, 0x0,ASC_BCLKDIV_16},
285 {16934400, 8000, 2112, 0x17, 0x0,ASC_BCLKDIV_16},
286 {8192000, 8000, 1024, 0x0, 0x0,ASC_BCLKDIV_16},
287 {12000000, 8000, 1500, 0x6, 0x1,ASC_BCLKDIV_16},
290 {11289600, 11025, 1024, 0x18, 0x0,ASC_BCLKDIV_16},
291 {16934400, 11025, 1536, 0x19, 0x0,ASC_BCLKDIV_16},
292 {12000000, 11025, 1088, 0x19, 0x1,ASC_BCLKDIV_16},
295 {12288000, 12000, 1024, 0x8, 0x0,ASC_BCLKDIV_16},
296 {18432000, 12000, 1536, 0x9, 0x0,ASC_BCLKDIV_16},
297 {12000000, 12000, 1000, 0x8, 0x1,ASC_BCLKDIV_16},
300 {12288000, 16000, 768, 0xa, 0x0,ASC_BCLKDIV_8},
301 {18432000, 16000, 1152, 0xb, 0x0,ASC_BCLKDIV_8},
302 {12000000, 16000, 750, 0xa, 0x1,ASC_BCLKDIV_8},
305 {11289600, 22050, 512, 0x1a, 0x0,ASC_BCLKDIV_8},
306 {16934400, 22050, 768, 0x1b, 0x0,ASC_BCLKDIV_8},
307 {12000000, 22050, 544, 0x1b, 0x1,ASC_BCLKDIV_8},
310 {12288000, 24000, 512, 0x1c, 0x0,ASC_BCLKDIV_8},
311 {18432000, 24000, 768, 0x1d, 0x0,ASC_BCLKDIV_8},
312 {12000000, 24000, 500, 0x1c, 0x1,ASC_BCLKDIV_8},
315 {12288000, 32000, 384, 0xc, 0x0,ASC_BCLKDIV_8},
316 {18432000, 32000, 576, 0xd, 0x0,ASC_BCLKDIV_8},
317 {12000000, 32000, 375, 0xa, 0x1,ASC_BCLKDIV_8},
320 {11289600, 44100, 256, 0x10, 0x0,ASC_BCLKDIV_8},
321 {16934400, 44100, 384, 0x11, 0x0,ASC_BCLKDIV_8},
322 {12000000, 44100, 272, 0x11, 0x1,ASC_BCLKDIV_8},
325 {12288000, 48000, 256, 0x0, 0x0,ASC_BCLKDIV_4},
326 {18432000, 48000, 384, 0x1, 0x0,ASC_BCLKDIV_4},
327 {12000000, 48000, 250, 0x0, 0x1,ASC_BCLKDIV_4},
330 {11289600, 88200, 128, 0x1e, 0x0,ASC_BCLKDIV_4},
331 {16934400, 88200, 192, 0x1f, 0x0,ASC_BCLKDIV_4},
332 {12000000, 88200, 136, 0x1f, 0x1,ASC_BCLKDIV_4},
335 {12288000, 96000, 128, 0xe, 0x0,ASC_BCLKDIV_4},
336 {18432000, 96000, 192, 0xf, 0x0,ASC_BCLKDIV_4},
337 {12000000, 96000, 125, 0xe, 0x1,ASC_BCLKDIV_4},
340 static inline int get_coeff(int mclk, int rate)
344 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
345 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
352 /* The set of rates we can generate from the above for each SYSCLK */
354 static unsigned int rates_12288[] = {
355 8000, 12000, 16000, 24000, 24000, 32000, 48000, 96000,
358 static struct snd_pcm_hw_constraint_list constraints_12288 = {
359 .count = ARRAY_SIZE(rates_12288),
363 static unsigned int rates_112896[] = {
364 8000, 11025, 22050, 44100,
367 static struct snd_pcm_hw_constraint_list constraints_112896 = {
368 .count = ARRAY_SIZE(rates_112896),
369 .list = rates_112896,
372 static unsigned int rates_12[] = {
373 8000, 11025, 12000, 16000, 22050, 2400, 32000, 41100, 48000,
377 static struct snd_pcm_hw_constraint_list constraints_12 = {
378 .count = ARRAY_SIZE(rates_12),
383 * Note that this should be called from init rather than from hw_params.
385 static int rk1000_codec_set_dai_sysclk(struct snd_soc_dai *codec_dai,
386 int clk_id, unsigned int freq, int dir)
388 struct snd_soc_codec *codec = codec_dai->codec;
389 struct rk1000_codec_priv *rk1000_codec = snd_soc_codec_get_drvdata(codec);
391 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
398 rk1000_codec->sysclk_constraints = &constraints_112896;
399 rk1000_codec->sysclk = freq;
406 rk1000_codec->sysclk_constraints = &constraints_12288;
407 rk1000_codec->sysclk = freq;
412 rk1000_codec->sysclk_constraints = &constraints_12;
413 rk1000_codec->sysclk = freq;
419 static int rk1000_codec_set_dai_fmt(struct snd_soc_dai *codec_dai,
422 struct snd_soc_codec *codec = codec_dai->codec;
425 /* set master/slave audio interface */
426 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
427 case SND_SOC_DAIFMT_CBM_CFM:
430 case SND_SOC_DAIFMT_CBS_CFS:
437 /* interface format */
438 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
439 case SND_SOC_DAIFMT_I2S:
442 case SND_SOC_DAIFMT_RIGHT_J:
444 case SND_SOC_DAIFMT_LEFT_J:
447 case SND_SOC_DAIFMT_DSP_A:
450 case SND_SOC_DAIFMT_DSP_B:
457 /* clock inversion */
458 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
459 case SND_SOC_DAIFMT_NB_NF:
461 case SND_SOC_DAIFMT_IB_IF:
464 case SND_SOC_DAIFMT_IB_NF:
467 case SND_SOC_DAIFMT_NB_IF:
474 DBG("Enter::%s----%d iface=%x\n",__FUNCTION__,__LINE__,iface);
475 rk1000_codec_write(codec, ACCELCODEC_R09, iface);
479 static int rk1000_codec_pcm_startup(struct snd_pcm_substream *substream,
480 struct snd_soc_dai *dai)
482 struct snd_soc_pcm_runtime *rtd = substream->private_data;
483 struct snd_soc_codec *codec = rtd->codec;
484 struct rk1000_codec_priv *rk1000_codec = snd_soc_codec_get_drvdata(codec);
486 /* The set of sample rates that can be supported depends on the
487 * MCLK supplied to the CODEC - enforce this.
489 DBG("Enter::%s----%d rk1000_codec->sysclk=%d\n",__FUNCTION__,__LINE__,rk1000_codec->sysclk);
490 // if (!rk1000_codec->sysclk) {
491 // dev_err(codec->dev,
492 // "No MCLK configured, call set_sysclk() on init\n");
497 snd_pcm_hw_constraint_list(substream->runtime, 0,
498 SNDRV_PCM_HW_PARAM_RATE,
499 rk1000_codec->sysclk_constraints);
504 static int rk1000_codec_pcm_hw_params(struct snd_pcm_substream *substream,
505 struct snd_pcm_hw_params *params,
506 struct snd_soc_dai *dai)
508 struct snd_soc_pcm_runtime *rtd = substream->private_data;
509 struct snd_soc_codec *codec = rtd->codec;
510 struct rk1000_codec_priv *rk1000_codec = snd_soc_codec_get_drvdata(codec);
512 u16 iface = rk1000_codec_read_reg_cache(codec, ACCELCODEC_R09) & 0x1f3;
513 u16 srate = rk1000_codec_read_reg_cache(codec, ACCELCODEC_R00) & 0x180;
516 rk1000_codec->sysclk = 12000000;
517 /*by Vincent Hsiung for EQ Vol Change*/
518 #define HW_PARAMS_FLAG_EQVOL_ON 0x21
519 #define HW_PARAMS_FLAG_EQVOL_OFF 0x22
520 if (params->flags == HW_PARAMS_FLAG_EQVOL_ON)
522 u16 r17 = rk1000_codec_read_reg_cache(codec, ACCELCODEC_R17);
523 u16 r18 = rk1000_codec_read_reg_cache(codec, ACCELCODEC_R18);
525 r17 &= (~0x3f); //6db
526 r18 &= (~0x3f); //6db
528 rk1000_codec_write(codec, ACCELCODEC_R17, r17);
529 rk1000_codec_write(codec, ACCELCODEC_R18, r18);
533 else if (params->flags == HW_PARAMS_FLAG_EQVOL_OFF)
535 u16 r17 = rk1000_codec_read_reg_cache(codec, ACCELCODEC_R17);
536 u16 r18 = rk1000_codec_read_reg_cache(codec, ACCELCODEC_R18);
544 rk1000_codec_write(codec, ACCELCODEC_R17, r17);
545 rk1000_codec_write(codec, ACCELCODEC_R18, r18);
549 coeff = get_coeff(rk1000_codec->sysclk, params_rate(params));
552 switch (params_format(params)) {
553 case SNDRV_PCM_FORMAT_S16_LE:
555 case SNDRV_PCM_FORMAT_S20_3LE:
558 case SNDRV_PCM_FORMAT_S24_LE:
561 case SNDRV_PCM_FORMAT_S32_LE:
565 DBG("Enter::%s----%d iface=%x srate =%x rate=%d\n",__FUNCTION__,__LINE__,iface,srate,params_rate(params));
567 rk1000_codec_write(codec,ACCELCODEC_R0C, 0x17);
568 rk1000_codec_write(codec,ACCELCODEC_R04, ASC_INT_MUTE_L|ASC_INT_MUTE_R|ASC_SIDETONE_L_OFF|ASC_SIDETONE_R_OFF); //soft mute
569 //±ØÐëÏȽ«clkºÍEN_INT¶¼disableµô£¬·ñÔòÇл»bclk·ÖƵֵ¿ÉÄܵ¼ÖÂcodecÄÚ²¿Ê±Ðò»ìÂÒµô£¬
570 //±íÏÖ³öÀ´µÄÏÖÏóÊÇ£¬ÒÔºóµÄÒôÀÖ¶¼±ä³ÉÁËÔëÒô£¬¶øÇÒ¾ÍËã°ÑÊäÈëcodecµÄI2S_DATAOUT¶Ï¿ªÒ²Ò»Ñù³öÔëÒô
571 rk1000_codec_write(codec,ACCELCODEC_R0B, ASC_DEC_DISABLE|ASC_INT_DISABLE); //0x00
573 /* set iface & srate */
574 rk1000_codec_write(codec, ACCELCODEC_R09, iface);
577 // rk1000_codec_write(codec, ACCELCODEC_R0A, (coeff_div[coeff].sr << 1) | coeff_div[coeff].usb|ASC_CLKNODIV|ASC_CLK_ENABLE);
578 rk1000_codec_write(codec, ACCELCODEC_R0A, 0xa0);
579 // rk1000_codec_write(codec, ACCELCODEC_R00, srate|coeff_div[coeff].bclk);
581 rk1000_codec_write(codec,ACCELCODEC_R0B, gR0BReg);
586 void PhaseOut(struct snd_soc_codec *codec,u32 nStep, u32 us)
588 DBG("%s[%d]\n",__FUNCTION__,__LINE__);
589 rk1000_codec_write(codec,ACCELCODEC_R17, 0x00|ASC_OUTPUT_ACTIVE|ASC_CROSSZERO_EN); //AOL
590 rk1000_codec_write(codec,ACCELCODEC_R18, 0x00|ASC_OUTPUT_ACTIVE|ASC_CROSSZERO_EN); //AOR
594 void PhaseIn(struct snd_soc_codec *codec,u32 nStep, u32 us)
596 DBG("%s[%d]\n",__FUNCTION__,__LINE__);
597 rk1000_codec_write(codec,ACCELCODEC_R17, 0x00|ASC_OUTPUT_ACTIVE|ASC_CROSSZERO_EN); //AOL gVolReg|ASC_OUTPUT_ACTIVE|ASC_CROSSZERO_EN); //AOL
598 rk1000_codec_write(codec,ACCELCODEC_R18, 0x00|ASC_OUTPUT_ACTIVE|ASC_CROSSZERO_EN); //gVolReg|ASC_OUTPUT_ACTIVE|ASC_CROSSZERO_EN); //AOR
602 static int rk1000_codec_mute(struct snd_soc_dai *dai, int mute)
604 struct snd_soc_codec *codec = dai->codec;
606 DBG("Enter::%s----%d--mute=%d\n",__FUNCTION__,__LINE__,mute);
610 PhaseOut(codec,1, 5000);
611 rk1000_codec_write(codec,ACCELCODEC_R19, 0xFF); //AOM
612 rk1000_codec_write(codec,ACCELCODEC_R04, ASC_INT_MUTE_L|ASC_INT_MUTE_R|ASC_SIDETONE_L_OFF|ASC_SIDETONE_R_OFF); //soft mute
616 rk1000_codec_write(codec,ACCELCODEC_R1D, 0x2a); //setup Vmid and Vref, other module power down
617 rk1000_codec_write(codec,ACCELCODEC_R1E, 0x40); ///|ASC_PDASDML_ENABLE);
618 rk1000_codec_write(codec,ACCELCODEC_R1F, 0x09|ASC_PDMIXM_ENABLE|ASC_PDPAM_ENABLE); ///|ASC_PDMICB_ENABLE|ASC_PDMIXM_ENABLE);
619 PhaseIn(codec,1, 5000);
620 // if(gCodecVol != 0)
622 rk1000_codec_write(codec,ACCELCODEC_R04, ASC_INT_ACTIVE_L|ASC_INT_ACTIVE_R|ASC_SIDETONE_L_OFF|ASC_SIDETONE_R_OFF);
624 rk1000_codec_write(codec,ACCELCODEC_R19, 0x7F); //AOM
627 rockchip_mux_api_set(SPK_IOMUX_PIN_NAME, SPK_IOMUX_PIN_DIR);
628 GPIOSetPinDirection(SPK_CTRL_PIN,GPIO_OUT);
629 GPIOSetPinLevel(SPK_CTRL_PIN,GPIO_HIGH);
635 static int rk1000_codec_set_bias_level(struct snd_soc_codec *codec,
636 enum snd_soc_bias_level level)
638 u16 pwr_reg = rk1000_codec_read_reg_cache(codec, ACCELCODEC_R1D) & ~0x1c1;
639 DBG("Enter::%s----%d level =%d\n",__FUNCTION__,__LINE__,level);
641 case SND_SOC_BIAS_ON:
644 case SND_SOC_BIAS_PREPARE:
645 /* VREF, VMID=2x50k, digital enabled */
646 rk1000_codec_write(codec, ACCELCODEC_R1D, pwr_reg | 0x0080);
649 case SND_SOC_BIAS_STANDBY:
650 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF) {
651 /* VREF, VMID=2x5k */
652 rk1000_codec_write(codec, ACCELCODEC_R1D, pwr_reg | 0x0080);
658 /* VREF, VMID=2*500k, digital stopped */
659 rk1000_codec_write(codec, ACCELCODEC_R1D, pwr_reg | 0x0080);
662 case SND_SOC_BIAS_OFF:
663 rk1000_codec_write(codec, ACCELCODEC_R1D, 0x0000);
666 codec->dapm.bias_level = level;
670 #define RK1000_CODEC_RATES SNDRV_PCM_RATE_8000_96000
672 #define RK1000_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
673 SNDRV_PCM_FMTBIT_S24_LE)
675 static struct snd_soc_dai_ops rk1000_codec_ops = {
676 .startup = rk1000_codec_pcm_startup,
677 .hw_params = rk1000_codec_pcm_hw_params,
678 .set_fmt = rk1000_codec_set_dai_fmt,
679 .set_sysclk = rk1000_codec_set_dai_sysclk,
680 .digital_mute = rk1000_codec_mute,
683 static struct snd_soc_dai_driver rk1000_codec_dai[] = {
685 .name = "rk1000_codec",
687 .stream_name = "Playback",
690 .rates = RK1000_CODEC_RATES,
691 .formats = RK1000_CODEC_FORMATS,
694 .stream_name = "Capture",
697 .rates = RK1000_CODEC_RATES,
698 .formats = RK1000_CODEC_FORMATS,
700 .ops = &rk1000_codec_ops,
701 .symmetric_rates = 1,
705 static int rk1000_codec_suspend(struct snd_soc_codec *codec, pm_message_t state)
707 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
708 rk1000_codec_set_bias_level(codec, SND_SOC_BIAS_OFF);
712 static int rk1000_codec_resume(struct snd_soc_codec *codec)
716 struct i2c_client *i2c;
717 u16 *cache = codec->reg_cache;
719 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
720 /* Sync reg_cache with the hardware */
721 for (i = 0; i < RK1000_CODEC_NUM_REG; i++) {
722 data[0] = cache[i] & 0x00ff;
723 i2c = (struct i2c_client *)codec->control_data;
724 i2c->addr = (i2c->addr & 0x60)|i;
725 codec->hw_write(codec->control_data, data, 1);
728 rk1000_codec_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
733 static void rk1000_reg_init(struct snd_soc_codec *codec)
735 rk1000_codec_write(codec,ACCELCODEC_R1D, 0x00);
736 rk1000_codec_write(codec,ACCELCODEC_R17, 0xFF); //AOL
737 rk1000_codec_write(codec,ACCELCODEC_R18, 0xFF); //AOR
738 rk1000_codec_write(codec,ACCELCODEC_R19, 0xFF); //AOM
740 rk1000_codec_write(codec,ACCELCODEC_R1F, 0xDF);
742 rk1000_codec_write(codec,ACCELCODEC_R1F, 0x5F);
743 rk1000_codec_write(codec,ACCELCODEC_R19, 0x7F); //AOM
744 rk1000_codec_write(codec,ACCELCODEC_R15, 0xC1);//rk1000_codec_write(codec,ACCELCODEC_R15, 0xCD);//by Vincent Hsiung
745 rk1000_codec_write(codec,ACCELCODEC_R1A, 0x1C);
747 rk1000_codec_write(codec,ACCELCODEC_R1F, 0x09);
748 rk1000_codec_write(codec,ACCELCODEC_R1E, 0x00);
750 rk1000_codec_write(codec,ACCELCODEC_R1A, 0x14);
751 rk1000_codec_write(codec,ACCELCODEC_R1D, 0xFE);
752 rk1000_codec_write(codec,ACCELCODEC_R17, 0xBF); //AOL
753 rk1000_codec_write(codec,ACCELCODEC_R18, 0xBF); //AOR
754 rk1000_codec_write(codec,ACCELCODEC_R19, 0x7F); //AOM
755 rk1000_codec_write(codec,ACCELCODEC_R1F, 0xDF);
758 rk1000_codec_write(codec,ACCELCODEC_R04, ASC_INT_MUTE_L|ASC_INT_MUTE_R|ASC_SIDETONE_L_OFF|ASC_SIDETONE_R_OFF); //soft mute
760 //2set default SR and clk
761 rk1000_codec_write(codec,ACCELCODEC_R0A, ASC_USB_MODE|FREQ48kHz|ASC_CLKNODIV|ASC_CLK_DISABLE);
762 gR0AReg = ASC_USB_MODE|FREQ48kHz|ASC_CLKNODIV|ASC_CLK_DISABLE;
763 //2Config audio interface
764 rk1000_codec_write(codec,ACCELCODEC_R09, ASC_I2S_MODE|ASC_16BIT_MODE|ASC_NORMAL_LRCLK|ASC_LRSWAP_DISABLE|ASC_MASTER_MODE|ASC_NORMAL_BCLK);
765 rk1000_codec_write(codec,ACCELCODEC_R00, ASC_HPF_ENABLE|ASC_DSM_MODE_DISABLE|ASC_SCRAMBLE_ENABLE|ASC_DITHER_ENABLE|ASC_BCLKDIV_8); //BCLK div 8
766 //2volume,input,outpu
767 rk1000_codec_write(codec,ACCELCODEC_R05, 0x0e);
768 rk1000_codec_write(codec,ACCELCODEC_R06, 0x42);
769 rk1000_codec_write(codec,ACCELCODEC_R07, 0x0e);
770 rk1000_codec_write(codec,ACCELCODEC_R08, 0x42);
772 rk1000_codec_write(codec,ACCELCODEC_R0C, 0x10|ASC_INPUT_VOL_0DB|ASC_INPUT_MUTE); //LIL
773 rk1000_codec_write(codec,ACCELCODEC_R0D, 0x10|ASC_INPUT_VOL_0DB); //LIR
774 rk1000_codec_write(codec,ACCELCODEC_R0E, 0x10|ASC_INPUT_VOL_0DB); //MIC
775 rk1000_codec_write(codec,ACCELCODEC_R12, 0x4c|ASC_MIC_INPUT|ASC_MIC_BOOST_20DB); //mic input and boost 20dB
776 rk1000_codec_write(codec,ACCELCODEC_R13, ASC_LPGAMX_DISABLE|ASC_ALMX_DISABLE|((LINE_2_MIXER_GAIN & 0x7) << 4)|0x0);
777 rk1000_codec_write(codec,ACCELCODEC_R14, ASC_RPGAMX_DISABLE|ASC_ARMX_DISABLE|((LINE_2_MIXER_GAIN & 0x7) << 4)|0x0);
778 gR1314Reg = ASC_RPGAMX_DISABLE|ASC_ARMX_DISABLE|((LINE_2_MIXER_GAIN & 0x7) << 4)|0x0;
781 rk1000_codec_write(codec,ACCELCODEC_R0B, ASC_DEC_DISABLE|ASC_INT_DISABLE); //0x00
782 gR0BReg = ASC_DEC_DISABLE|ASC_INT_DISABLE;
783 rk1000_codec_write(codec,ACCELCODEC_R15, \
784 0x01|ASC_RLPFMX_DISABLE|ASC_LLPFMX_DISABLE|ASC_LDAMX_DISABLE|ASC_RDAMX_DISABLE|ASC_LSCF_ACTIVE|ASC_RSCF_ACTIVE); //0x3c
785 rk1000_codec_write(codec,ACCELCODEC_R1B, 0x32);
786 rk1000_codec_write(codec,ACCELCODEC_R1C, ASC_DEM_ENABLE); ///0x00); //use default value
789 rk1000_codec_write(codec,ACCELCODEC_R17, 0xBF); //AOL ÒôÁ¿×îµÍ
790 rk1000_codec_write(codec,ACCELCODEC_R18, 0xBF); //AOR
792 //2power down useless module
793 rk1000_codec_write(codec,ACCELCODEC_R1D, 0x2a|ASC_PDSDL_ENABLE|ASC_PDBSTL_ENABLE|ASC_PDPGAL_ENABLE); //setup Vmid and Vref, other module power down
794 rk1000_codec_write(codec,ACCELCODEC_R1E, 0x40|ASC_PDASDML_ENABLE);
796 rk1000_codec_write(codec,ACCELCODEC_R1F, 0x09|ASC_PDMICB_ENABLE|ASC_PDMIXM_ENABLE);
798 rk1000_codec_write(codec,ACCELCODEC_R1F, 0x09|ASC_PDMICB_ENABLE|ASC_PDMIXM_ENABLE|ASC_PDPAM_ENABLE);
802 rk1000_codec_write(codec,ACCELCODEC_R0B, ASC_DEC_DISABLE|ASC_INT_ENABLE);
803 gR0BReg = ASC_DEC_ENABLE|ASC_INT_ENABLE; //ASC_DEC_DISABLE|ASC_INT_ENABLE;
804 rk1000_codec_write(codec,ACCELCODEC_R15, 0xC1);//rk1000_codec_write(codec,ACCELCODEC_R15, 0xCD);//by Vincent Hsiung
805 rk1000_codec_write(codec,ACCELCODEC_R0C, 0x10|ASC_INPUT_VOL_0DB|ASC_INPUT_MUTE); //LIL
806 rk1000_codec_write(codec,ACCELCODEC_R0D, 0x10|ASC_INPUT_VOL_0DB); //LIR
807 rk1000_codec_write(codec,ACCELCODEC_R0E, 0x10|ASC_INPUT_VOL_0DB); //MIC
808 rk1000_codec_write(codec,ACCELCODEC_R12, 0x4c|ASC_MIC_INPUT|ASC_MIC_BOOST_20DB); //mic input and boost 20dB
809 rk1000_codec_write(codec,ACCELCODEC_R13, 0x00);
810 rk1000_codec_write(codec,ACCELCODEC_R14, 0x00);
812 rk1000_codec_write(codec,ACCELCODEC_R1C, ASC_DEM_ENABLE); //0x00); //use default value
815 static int rk1000_codec_probe(struct snd_soc_codec *codec)
817 struct rk1000_codec_priv *rk1000_codec_priv = snd_soc_codec_get_drvdata(codec);
820 DBG("%s::%d\n",__FUNCTION__,__LINE__);
822 rk1000_codec_codec = codec;
824 codec->control_data = rk1000_codec_priv->control_data;
826 ret = snd_soc_codec_set_cache_io(codec, 8, 8, rk1000_codec_priv->control_type);
828 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
832 codec->reg_cache = kmemdup(rk1000_codec_reg, sizeof(rk1000_codec_reg), GFP_KERNEL);
833 if (codec->reg_cache == NULL)
836 rk1000_reg_init(codec);
837 // snd_soc_add_controls(codec, rk1000_codec_snd_controls,
838 // ARRAY_SIZE(rk1000_codec_snd_controls));
839 // snd_soc_dapm_new_controls(codec, rk1000_codec_dapm_widgets,
840 // ARRAY_SIZE(rk1000_codec_dapm_widgets));
841 // snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
847 static int rk1000_codec_remove(struct snd_soc_codec *codec)
849 struct rk1000_codec_priv *rk1000_codec_priv = snd_soc_codec_get_drvdata(codec);
851 rk1000_codec_set_bias_level(codec, SND_SOC_BIAS_OFF);
852 kfree(rk1000_codec_priv);
856 static struct snd_soc_codec_driver soc_codec_dev_rk1000_codec = {
857 .probe = rk1000_codec_probe,
858 .remove = rk1000_codec_remove,
859 .suspend = rk1000_codec_suspend,
860 .resume = rk1000_codec_resume,
861 .set_bias_level = rk1000_codec_set_bias_level,
862 .read = rk1000_codec_read,
863 .write = rk1000_codec_write,
864 // .readable_register = rk1000_codec_read_reg_cache,
865 // .writable_register = rk1000_codec_write_reg_cache,
866 // .volatile_register = wm8994_volatile,
867 .reg_cache_size = ARRAY_SIZE(rk1000_codec_reg),
868 .reg_word_size = sizeof(u8),
869 .reg_cache_default = rk1000_codec_reg,
872 #ifdef RK1000_CODEC_PROC
873 static int rk1000_codec_proc_init(void);
876 static __devinit int rk1000_codec_i2c_probe(struct i2c_client *i2c,
877 const struct i2c_device_id *id)
879 struct rk1000_codec_priv *rk1000_codec;
881 DBG("%s::%d\n",__FUNCTION__,__LINE__);
882 rk1000_codec = kzalloc(sizeof(struct rk1000_codec_priv), GFP_KERNEL);
883 if (rk1000_codec == NULL)
886 i2c_set_clientdata(i2c, rk1000_codec);
887 rk1000_codec->control_type = SND_SOC_I2C;
888 rk1000_codec->control_data = i2c;
890 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rk1000_codec,
891 rk1000_codec_dai, ARRAY_SIZE(rk1000_codec_dai));
895 #ifdef RK1000_CODEC_PROC
896 rk1000_codec_proc_init();
902 static __devexit int rk1000_codec_i2c_remove(struct i2c_client *client)
904 snd_soc_unregister_codec(&client->dev);
905 kfree(i2c_get_clientdata(client));
909 static const struct i2c_device_id rk1000_codec_i2c_id[] = {
910 { "rk1000_i2c_codec", 0 },
913 MODULE_DEVICE_TABLE(i2c, rk1000_codec_i2c_id);
915 /* corgi i2c codec control layer */
916 static struct i2c_driver rk1000_codec_i2c_driver = {
918 .name = "RK1000_CODEC",
919 .owner = THIS_MODULE,
921 .probe = rk1000_codec_i2c_probe,
922 .remove = __devexit_p(rk1000_codec_i2c_remove),
923 .id_table = rk1000_codec_i2c_id,
927 static int __init rk1000_codec_modinit(void)
929 DBG("%s::%d\n",__FUNCTION__,__LINE__);
930 return i2c_add_driver(&rk1000_codec_i2c_driver);
932 module_init(rk1000_codec_modinit);
934 static void __exit rk1000_codec_exit(void)
936 i2c_del_driver(&rk1000_codec_i2c_driver);
938 module_exit(rk1000_codec_exit);
940 #ifdef RK1000_CODEC_PROC
941 void rk1000_codec_reg_read(void)
943 struct snd_soc_codec *codec = rk1000_codec_codec;
947 for (i=0; i<=0x1f; i++){
948 data = rk1000_codec_read(codec, i);
949 printk("reg[0x%x]=0x%x\n",i,data);
953 static ssize_t rk1000_codec_proc_write(struct file *file, const char __user *buffer,
954 unsigned long len, void *data)
961 cookie_pot = (char *)vmalloc( len );
968 if (copy_from_user( cookie_pot, buffer, len ))
972 switch(cookie_pot[0])
977 debug_write_read %= 2;
978 if(debug_write_read != 0)
979 printk("Debug read and write reg on\n");
981 printk("Debug read and write reg off\n");
985 printk("Read reg debug\n");
986 if(cookie_pot[1] ==':')
988 debug_write_read = 1;
989 strsep(&cookie_pot,":");
990 while((p=strsep(&cookie_pot,",")))
992 reg = simple_strtol(p,NULL,16);
993 value = rk1000_codec_read(rk1000_codec_codec,reg);
994 printk("rk1000_codec_read:0x%04x = 0x%04x",reg,value);
996 debug_write_read = 0;
1001 printk("Error Read reg debug.\n");
1002 printk("For example: echo 'r:22,23,24,25'>wm8994_ts\n");
1007 printk("Write reg debug\n");
1008 if(cookie_pot[1] ==':')
1010 debug_write_read = 1;
1011 strsep(&cookie_pot,":");
1012 while((p=strsep(&cookie_pot,"=")))
1014 reg = simple_strtol(p,NULL,16);
1015 p=strsep(&cookie_pot,",");
1016 value = simple_strtol(p,NULL,16);
1017 rk1000_codec_write(rk1000_codec_codec,reg,value);
1018 printk("rk1000_codec_write:0x%04x = 0x%04x\n",reg,value);
1020 debug_write_read = 0;
1025 printk("Error Write reg debug.\n");
1026 printk("For example: w:22=0,23=0,24=0,25=0\n");
1029 case 'p'://enable pa
1030 rk1000_codec_reg_read();
1033 printk("Help for rk1000_codec_ts .\n-->The Cmd list: \n");
1034 printk("-->'d&&D' Open or Off the debug\n");
1035 printk("-->'r&&R' Read reg debug,Example: echo 'r:22,23,24,25'>rk1000_codec_ts\n");
1036 printk("-->'w&&W' Write reg debug,Example: echo 'w:22=0,23=0,24=0,25=0'>rk1000_codec_ts\n");
1042 static const struct file_operations rk1000_codec_proc_fops = {
1043 .owner = THIS_MODULE,
1044 //.open = snd_mem_proc_open,
1047 // .write = rk1000_codec_proc_write,
1049 //.llseek = seq_lseek,
1050 //.release = single_release,
1053 static int rk1000_codec_proc_init(void)
1055 struct proc_dir_entry *rk1000_codec_proc_entry;
1056 rk1000_codec_proc_entry = create_proc_entry("driver/rk1000_codec", 0777, NULL);
1057 if(rk1000_codec_proc_entry != NULL)
1059 rk1000_codec_proc_entry->write_proc = rk1000_codec_proc_write;
1064 printk("create rk1000_codec proc error !\n");
1072 int reg_send_data(struct i2c_client *client, const char start_reg,
1073 const char *buf, int count, unsigned int scl_rate)
1076 struct i2c_adapter *adap = client->adapter;
1078 char tx_buf[count + 1];
1080 tx_buf[0] = start_reg;
1081 memcpy(tx_buf+1, buf, count);
1083 msg.addr = client->addr;
1086 msg.flags = client->flags;
1087 msg.scl_rate = scl_rate;
1089 ret = i2c_transfer(adap, &msg, 1);
1094 static int rk1000_control_probe(struct i2c_client *client,
1095 const struct i2c_device_id *id)
1098 char data[4] = {0x88, 0x0d, 0x22, 0x00};
1099 // reg[0x00] = 0x88, --> ADC_CON
1100 // reg[0x01] = 0x0d, --> CODEC_CON
1101 // reg[0x02] = 0x22, --> I2C_CON
1102 // reg[0x03] = 0x00, --> TVE_CON
1103 #ifdef CONFIG_SND_SOC_RK1000
1107 DBG("%s::%d\n",__FUNCTION__,__LINE__);
1108 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
1110 dev_err(&client->dev, "i2c bus does not support the rk1000_control\n");
1115 ret = reg_send_data(client, 0x00, data, 4, 100 * 1000);
1117 printk("i2c write ret = 0x%x\n",ret);
1118 memset(data,0,sizeof(data));
1119 ret = i2c_master_reg8_recv(client, 0, data, (int)4, 20*1000);
1120 printk("i2c read reg %x, %x, %x, %x ret=x%x\n",data[0],data[1],data[2],data[3],ret);
1129 static int rk1000_control_remove(struct i2c_client *client)
1134 static const struct i2c_device_id rk1000_control_id[] = {
1135 { "rk1000_control", 0 },
1138 MODULE_DEVICE_TABLE(i2c, rk1000_control_id);
1140 static struct i2c_driver rk1000_control_driver = {
1142 .name = "rk1000_control",
1144 .probe = rk1000_control_probe,
1145 .remove = rk1000_control_remove,
1146 .id_table = rk1000_control_id,
1149 static int __init rk1000_control_init(void)
1151 return i2c_add_driver(&rk1000_control_driver);
1154 static void __exit rk1000_control_exit(void)
1156 i2c_del_driver(&rk1000_control_driver);
1159 module_init(rk1000_control_init);
1160 module_exit(rk1000_control_exit);
1162 MODULE_DESCRIPTION("ASoC RK1000 CODEC driver");
1163 MODULE_AUTHOR("lhh lhh@rock-chips.com");
1164 MODULE_LICENSE("GPL");