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.
13 #include <linux/vmalloc.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
20 #include <linux/i2c.h>
21 #include <linux/platform_device.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/initval.h>
29 #include <linux/of_gpio.h>
30 #include "rk1000_codec.h"
36 #define RK1000_CODEC_DBG 0
38 #define DBG(x...) pr_info(x)
43 /*rk1000 codec proc debug*/
44 #define RK1000_CODEC_PROC 1
51 #define FREQ441KHZ (0x11 << 1)
52 /* rk1000 output volume,DAC Digital Gain */
54 #define VOLUME_OUTPUT 0xF42
55 /* 0x0 ~ 0x3f(bit0-bit5) max=0x0(+6DB) min=0x3f(-60DB) Analog Gain */
56 #define VOLUME_CODEC_PA 0x0
58 /* rk1000 input volume,rk610 can not adjust the recording volume */
59 #define VOLUME_INPUT 0x07
61 #define OUT_CAPLESS (1)
63 /* 1:set pll from rk1000 */
64 #define RK1000_CTL_PLL 0
70 * rk1000 register cache
71 * We can't read the RK1000 register space when we
72 * are using 2 wire for device control, so we cache them instead.
74 static const u16 rk1000_codec_reg[] = {
75 0x0005, 0x0004, 0x00fd, 0x00f3, /* 0 */
76 0x0003, 0x0000, 0x0000, 0x0000, /* 4 */
77 0x0000, 0x0005, 0x0000, 0x0000, /* 8 */
78 0x0097, 0x0097, 0x0097, 0x0097, /* 0x0a */
79 0x0097, 0x0097, 0x00cc, 0x0000, /* 0x10 */
80 0x0000, 0x00f1, 0x0090, 0x00ff, /* 0x14 */
81 0x00ff, 0x00ff, 0x009c, 0x0000, /* 0x18 */
82 0x0000, 0x00ff, 0x00ff, 0x00ff, /* 0x1a */
85 static struct snd_soc_codec *rk1000_codec_codec;
86 /* codec private data */
87 struct rk1000_codec_priv {
88 enum snd_soc_control_type control_type;
90 struct snd_soc_codec codec;
91 struct snd_pcm_hw_constraint_list *sysclk_constraints;
92 u16 reg_cache[RK1000_CODEC_NUM_REG];
94 struct delayed_work rk1000_delayed_work;
95 unsigned int spk_ctrl_io;
96 enum of_gpio_flags flags;
98 Some amplifiers enable a longer time.
99 config after pa_enable_io delay pa_enable_time(ms)
100 so value range is 0 - 8000.
102 unsigned int pa_enable_time;
103 /* if found boot pop,set boot_depop 1 test */
110 static void spk_ctrl_fun(int status)
112 struct rk1000_codec_priv *rk1000_codec;
114 if (rk1000_codec_codec == NULL)
116 rk1000_codec = snd_soc_codec_get_drvdata(rk1000_codec_codec);
117 if (rk1000_codec == NULL)
119 if (!rk1000_codec->spk_ctrl_io)
121 DBG("%s:: spk status = %d\n", __func__, status);
123 gpio_set_value(rk1000_codec->spk_ctrl_io,
124 rk1000_codec->flags);
126 gpio_set_value(rk1000_codec->spk_ctrl_io,
127 !rk1000_codec->flags);
132 * read rk1000 register cache
134 static unsigned int rk1000_codec_read_reg_cache(struct snd_soc_codec *codec,
137 u16 *cache = codec->reg_cache;
139 if (reg > RK1000_CACHE_REGNUM)
144 static unsigned int rk1000_codec_read(struct snd_soc_codec *codec,
147 struct i2c_msg xfer[1];
150 struct i2c_client *i2c;
153 i2c = to_i2c_client(codec->dev);
155 xfer[0].addr = (i2c->addr & 0x60) | (reg);
156 xfer[0].flags = I2C_M_RD;
158 xfer[0].buf = (unsigned char *)®
159 xfer[0].scl_rate = 100000;
160 ret = i2c_transfer(i2c->adapter, xfer, 1);
162 dev_err(&i2c->dev, "i2c_transfer() returned %d\n", ret);
169 * write rk1000 register cache
171 static inline void rk1000_codec_write_reg_cache(struct snd_soc_codec *codec,
177 cache = codec->reg_cache;
178 if (reg > RK1000_CACHE_REGNUM)
183 static int rk1000_codec_write(struct snd_soc_codec *codec, unsigned int reg,
186 #ifdef CONFIG_MODEM_SOUND
187 struct rk1000_codec_priv *rk1000_codec;
190 struct i2c_client *i2c;
192 i2c = to_i2c_client(codec->dev);
193 #ifdef CONFIG_MODEM_SOUND
194 rk1000_codec = snd_soc_codec_get_drvdata(rk1000_codec_codec);
195 if (rk1000_codec->call_enable)
198 DBG("Enter::%s, %d, reg=0x%02X, value=0x%02X\n", __func__,
199 __LINE__, reg, value);
200 data[0] = value & 0x00ff;
201 i2c->addr = (i2c->addr & 0x60)|reg;
202 if (codec->hw_write(i2c, data, 1) == 1) {
203 DBG("====%s %d Run OK=======\n", __func__, __LINE__);
204 rk1000_codec_write_reg_cache(codec, reg, value);
207 DBG("====%s %d Run EIO=====\n", __func__, __LINE__);
212 #ifdef CONFIG_MODEM_SOUND
213 static int rk1000_codec_write_incall(struct snd_soc_codec *codec,
214 unsigned int reg, unsigned int value)
217 struct i2c_client *i2c;
219 DBG("Enter::%s, %d, reg=0x%02X, value=0x%02X\n",
220 __func__, __LINE__, reg, value);
221 i2c = to_i2c_client(codec->dev);
222 data[0] = value & 0x00ff;
223 rk1000_codec_write_reg_cache(codec, reg, value);
224 i2c = (struct i2c_client *)codec->control_data;
225 i2c->addr = (i2c->addr & 0x60) | reg;
226 if (codec->hw_write(i2c, data, 1) == 1)
232 void call_set_spk(int on)
234 struct rk1000_codec_priv *rk1000_codec;
236 if (!rk1000_codec_codec)
238 rk1000_codec = snd_soc_codec_get_drvdata(rk1000codec_codec);
244 /* modem exit call,codec disable loopback */
245 DBG("%s modem exit call\n", __func__);
246 rk1000_codec_write_incall(rk1000_codec_codec,
247 ACCELCODEC_R0E, 0x80);
248 rk1000_codec->call_enable = 0;
251 /* modem calling,codec enable loopback,
252 * spk hp different volume */
253 DBG("%s spk incalling\n", __func__);
254 rk1000_codec->call_enable = 1;
255 rk1000_codec_write_incall(rk1000_codec_codec,
256 ACCELCODEC_R0E, 0x00);
259 DBG("%s hp incalling\n", __func__);
260 rk1000_codec->call_enable = 1;
261 rk1000_codec_write_incall(rk1000_codec_codec,
262 ACCELCODEC_R0E, 0x00);
265 DBG("%s bt incalling\n", __func__);
266 rk1000_codec->call_enable = 1;
267 rk1000_codec_write_incall(rk1000_codec_codec,
268 ACCELCODEC_R0E, 0x00);
285 /* codec hifi mclk clock divider coefficients */
286 static const struct _coeff_div coeff_div[] = {
288 {12288000, 8000, 1536, 0x6, 0x0, ASC_BCLKDIV_16},
289 {11289600, 8000, 1408, 0x16, 0x0, ASC_BCLKDIV_16},
290 {18432000, 8000, 2304, 0x7, 0x0, ASC_BCLKDIV_16},
291 {16934400, 8000, 2112, 0x17, 0x0, ASC_BCLKDIV_16},
292 {8192000, 8000, 1024, 0x0, 0x0, ASC_BCLKDIV_16},
293 {12000000, 8000, 1500, 0x6, 0x1, ASC_BCLKDIV_16},
295 {11289600, 11025, 1024, 0x18, 0x0, ASC_BCLKDIV_16},
296 {16934400, 11025, 1536, 0x19, 0x0, ASC_BCLKDIV_16},
297 {12000000, 11025, 1088, 0x19, 0x1, ASC_BCLKDIV_16},
299 {12288000, 12000, 1024, 0x8, 0x0, ASC_BCLKDIV_16},
300 {18432000, 12000, 1536, 0x9, 0x0, ASC_BCLKDIV_16},
301 {12000000, 12000, 1000, 0x8, 0x1, ASC_BCLKDIV_16},
303 {12288000, 16000, 768, 0xa, 0x0, ASC_BCLKDIV_8},
304 {18432000, 16000, 1152, 0xb, 0x0, ASC_BCLKDIV_8},
305 {12000000, 16000, 750, 0xa, 0x1, ASC_BCLKDIV_8},
307 {11289600, 22050, 512, 0x1a, 0x0, ASC_BCLKDIV_8},
308 {16934400, 22050, 768, 0x1b, 0x0, ASC_BCLKDIV_8},
309 {12000000, 22050, 544, 0x1b, 0x1, ASC_BCLKDIV_8},
311 {12288000, 24000, 512, 0x1c, 0x0, ASC_BCLKDIV_8},
312 {18432000, 24000, 768, 0x1d, 0x0, ASC_BCLKDIV_8},
313 {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},
319 {11289600, 44100, 256, 0x10, 0x0, ASC_BCLKDIV_4},
320 {16934400, 44100, 384, 0x11, 0x0, ASC_BCLKDIV_8},
321 {12000000, 44100, 272, 0x11, 0x1, ASC_BCLKDIV_8},
323 {12288000, 48000, 256, 0x0, 0x0, ASC_BCLKDIV_4},
324 {18432000, 48000, 384, 0x1, 0x0, ASC_BCLKDIV_4},
325 {12000000, 48000, 250, 0x0, 0x1, ASC_BCLKDIV_4},
327 {11289600, 88200, 128, 0x1e, 0x0, ASC_BCLKDIV_4},
328 {16934400, 88200, 192, 0x1f, 0x0, ASC_BCLKDIV_4},
329 {12000000, 88200, 136, 0x1f, 0x1, ASC_BCLKDIV_4},
331 {12288000, 96000, 128, 0xe, 0x0, ASC_BCLKDIV_4},
332 {18432000, 96000, 192, 0xf, 0x0, ASC_BCLKDIV_4},
333 {12000000, 96000, 125, 0xe, 0x1, ASC_BCLKDIV_4},
336 static inline int get_coeff(int mclk, int rate)
340 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
341 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
347 /* The set of rates we can generate from the above for each SYSCLK */
349 static unsigned int rates_12288[] = {
350 8000, 12000, 16000, 24000, 24000, 32000, 48000, 96000,
353 static struct snd_pcm_hw_constraint_list constraints_12288 = {
354 .count = ARRAY_SIZE(rates_12288),
358 static unsigned int rates_112896[] = {
359 8000, 11025, 22050, 44100,
362 static struct snd_pcm_hw_constraint_list constraints_112896 = {
363 .count = ARRAY_SIZE(rates_112896),
364 .list = rates_112896,
367 static unsigned int rates_12[] = {
368 8000, 11025, 12000, 16000, 22050, 2400, 32000, 41100, 48000,
372 static struct snd_pcm_hw_constraint_list constraints_12 = {
373 .count = ARRAY_SIZE(rates_12),
377 static int rk1000_codec_set_bias_level(struct snd_soc_codec *codec,
378 enum snd_soc_bias_level level)
380 DBG("Enter::%s----%d now_level =%d old_level = %d\n",
381 __func__, __LINE__, level, codec->dapm.bias_level);
383 case SND_SOC_BIAS_ON:
385 case SND_SOC_BIAS_PREPARE:
386 rk1000_codec_write(codec, ACCELCODEC_R1D, 0x2a);
387 rk1000_codec_write(codec, ACCELCODEC_R1E, 0x40);
388 rk1000_codec_write(codec, ACCELCODEC_R1F, 0x49);
389 /*VREF, VMID=2x50k, digital enabled */
392 case SND_SOC_BIAS_STANDBY:
393 DBG("rk1000 standby\n");
394 rk1000_codec_write(codec, ACCELCODEC_R1D, 0xFF);
395 rk1000_codec_write(codec, ACCELCODEC_R1E, 0xFF);
396 rk1000_codec_write(codec, ACCELCODEC_R1F, 0xFF);
399 case SND_SOC_BIAS_OFF:
400 DBG("rk1000 power off\n");
401 spk_ctrl_fun(GPIO_LOW);
402 rk1000_codec_write(codec, ACCELCODEC_R1D, 0xFF);
403 rk1000_codec_write(codec, ACCELCODEC_R1E, 0xFF);
404 rk1000_codec_write(codec, ACCELCODEC_R1F, 0xFF);
407 codec->dapm.bias_level = level;
411 * Note that this should be called from init rather than from hw_params.
413 static int rk1000_codec_set_dai_sysclk(struct snd_soc_dai *codec_dai,
414 int clk_id, unsigned int freq, int dir)
416 struct snd_soc_codec *codec = codec_dai->codec;
417 struct rk1000_codec_priv *rk1000_codec;
419 DBG("Enter::%s----%d\n", __func__, __LINE__);
420 rk1000_codec = snd_soc_codec_get_drvdata(codec);
426 rk1000_codec->sysclk_constraints = &constraints_112896;
427 rk1000_codec->sysclk = freq;
434 rk1000_codec->sysclk_constraints = &constraints_12288;
435 rk1000_codec->sysclk = freq;
440 rk1000_codec->sysclk_constraints = &constraints_12;
441 rk1000_codec->sysclk = freq;
447 static int rk1000_codec_set_dai_fmt(struct snd_soc_dai *codec_dai,
450 struct snd_soc_codec *codec = codec_dai->codec;
451 struct rk1000_codec_priv *rk1000_codec;
454 rk1000_codec = snd_soc_codec_get_drvdata(codec);
455 /* setup Vmid and Vref, other module power down */
456 rk1000_codec_write(codec, ACCELCODEC_R1D, 0x2a);
457 rk1000_codec_write(codec, ACCELCODEC_R1E, 0x40);
458 /* set master/slave audio interface */
459 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
460 case SND_SOC_DAIFMT_CBM_CFM:
463 case SND_SOC_DAIFMT_CBS_CFS:
469 /* interface format */
470 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
471 case SND_SOC_DAIFMT_I2S:
474 case SND_SOC_DAIFMT_RIGHT_J:
476 case SND_SOC_DAIFMT_LEFT_J:
479 case SND_SOC_DAIFMT_DSP_A:
482 case SND_SOC_DAIFMT_DSP_B:
488 /* clock inversion */
489 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
490 case SND_SOC_DAIFMT_NB_NF:
492 case SND_SOC_DAIFMT_IB_IF:
495 case SND_SOC_DAIFMT_IB_NF:
498 case SND_SOC_DAIFMT_NB_IF:
504 DBG("Enter::%s----%d iface=%x\n", __func__, __LINE__, iface);
505 rk1000_codec_write(codec, ACCELCODEC_R09, iface);
511 static int rk1000_codec_pcm_hw_params(struct snd_pcm_substream *substream,
512 struct snd_pcm_hw_params *params,
513 struct snd_soc_dai *dai)
517 struct snd_soc_pcm_runtime *rtd;
518 struct snd_soc_codec *codec;
519 unsigned int dai_fmt;
521 rtd = substream->private_data;
523 dai_fmt = rtd->card->dai_link[0].dai_fmt;
524 iface = rk1000_codec_read_reg_cache(codec, ACCELCODEC_R09) & 0x1f3;
526 rk1000_codec_write(codec, ACCELCODEC_R0C, 0x17);
527 rk1000_codec_write(codec, ACCELCODEC_R04, ASC_INT_MUTE_L|ASC_INT_MUTE_R|
528 ASC_SIDETONE_L_OFF|ASC_SIDETONE_R_OFF);
529 rk1000_codec_write(codec, ACCELCODEC_R0B,
530 ASC_DEC_DISABLE|ASC_INT_DISABLE);
531 /* set iface & srate */
532 if ((dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBM_CFM)
533 iface |= ASC_INVERT_BCLK;
534 rk1000_codec_write(codec, ACCELCODEC_R09, iface);
536 rk1000_codec_write(codec, ACCELCODEC_R0A, 0xa0);
537 rk1000_codec_write(codec, ACCELCODEC_R0B, g_r0_b_reg);
543 static int rk1000_codec_mute(struct snd_soc_dai *dai, int mute)
545 struct snd_soc_codec *codec = dai->codec;
546 struct rk1000_codec_priv *rk1000_codec;
548 DBG("Enter::%s----%d--mute=%d\n", __func__, __LINE__, mute);
549 rk1000_codec = snd_soc_codec_get_drvdata(codec);
552 rk1000_codec_write(codec, ACCELCODEC_R17, 0xFF);
554 rk1000_codec_write(codec, ACCELCODEC_R18, 0xFF);
556 rk1000_codec_write(codec, ACCELCODEC_R19, 0xFF);
558 rk1000_codec_write(codec, ACCELCODEC_R04, ASC_INT_MUTE_L|
559 ASC_INT_MUTE_R | ASC_SIDETONE_L_OFF |
562 /* setup Vmid and Vref, other module power down */
563 rk1000_codec_write(codec, ACCELCODEC_R1D, 0x2a);
564 rk1000_codec_write(codec, ACCELCODEC_R1E, 0x40);
566 rk1000_codec_write(codec, ACCELCODEC_R17, VOLUME_CODEC_PA |
567 ASC_OUTPUT_ACTIVE | ASC_CROSSZERO_EN);
569 rk1000_codec_write(codec, ACCELCODEC_R18, VOLUME_CODEC_PA |
570 ASC_OUTPUT_ACTIVE | ASC_CROSSZERO_EN);
571 rk1000_codec_write(codec, ACCELCODEC_R04, ASC_INT_ACTIVE_L|
572 ASC_INT_ACTIVE_R | ASC_SIDETONE_L_OFF|
575 rk1000_codec_write(codec, ACCELCODEC_R19, 0x7F);
577 rk1000_codec_write(codec, ACCELCODEC_R1F, 0x09|
580 rk1000_codec_write(codec, ACCELCODEC_R1F, 0x09|
581 ASC_PDMIXM_ENABLE | ASC_PDPAM_ENABLE);
587 static void rk1000_delayedwork_fun(struct work_struct *work)
589 struct snd_soc_codec *codec;
590 struct rk1000_codec_priv *rk1000_codec;
592 DBG("--------%s----------\n", __func__);
593 codec = rk1000_codec_codec;
594 rk1000_codec = snd_soc_codec_get_drvdata(codec);
595 if (!rk1000_codec->boot_depop) {
597 rk1000_codec_write(codec, ACCELCODEC_R1F,
598 0x09 | ASC_PDMIXM_ENABLE);
600 rk1000_codec_write(codec, ACCELCODEC_R1F,
601 0x09 | ASC_PDMIXM_ENABLE | ASC_PDPAM_ENABLE);
604 spk_ctrl_fun(GPIO_HIGH);
609 static struct snd_soc_dai_ops rk1000_codec_ops = {
610 .hw_params = rk1000_codec_pcm_hw_params,
611 .set_fmt = rk1000_codec_set_dai_fmt,
612 .set_sysclk = rk1000_codec_set_dai_sysclk,
613 .digital_mute = rk1000_codec_mute,
616 #define RK1000_CODEC_RATES SNDRV_PCM_RATE_8000_192000
617 #define RK1000_CODEC_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
618 SNDRV_PCM_FMTBIT_S20_3LE |\
619 SNDRV_PCM_FMTBIT_S24_LE)
620 static struct snd_soc_dai_driver rk1000_codec_dai[] = {
622 .name = "rk1000_codec",
624 .stream_name = "Playback",
627 .rates = RK1000_CODEC_RATES,
628 .formats = RK1000_CODEC_FORMATS,
631 .stream_name = "Capture",
634 .rates = RK1000_CODEC_RATES,
635 .formats = RK1000_CODEC_FORMATS,
637 .ops = &rk1000_codec_ops,
638 .symmetric_rates = 1,
643 void rk1000_codec_reg_set(void)
645 struct snd_soc_codec *codec;
646 struct rk1000_codec_priv *rk1000_codec;
647 unsigned int digital_gain;
648 unsigned int mic_vol;
650 mic_vol = VOLUME_INPUT;
651 codec = rk1000_codec_codec;
652 rk1000_codec = snd_soc_codec_get_drvdata(codec);
653 rk1000_codec_write(codec, ACCELCODEC_R1D, 0x30);
654 rk1000_codec_write(codec, ACCELCODEC_R1E, 0x40);
656 /*Route R-LPF->R-Mixer, L-LPF->L-Mixer*/
657 rk1000_codec_write(codec, ACCELCODEC_R15, 0xC1);
659 /*Route RDAC->R-Mixer, LDAC->L->Mixer*/
660 rk1000_codec_write(codec, ACCELCODEC_R15, 0x0C);
662 /*With Cap Output, VMID ramp up slow*/
663 rk1000_codec_write(codec, ACCELCODEC_R1A, 0x14);
665 rk1000_codec_write(codec, ACCELCODEC_R0C, 0x10 | ASC_INPUT_VOL_0DB);
666 rk1000_codec_write(codec, ACCELCODEC_R0D, 0x10 | ASC_INPUT_VOL_0DB);
668 if (mic_vol > 0x07) {
670 rk1000_codec_write(codec, ACCELCODEC_R12,
671 0x4c | ASC_MIC_INPUT | ASC_MIC_BOOST_20DB);
675 rk1000_codec_write(codec, ACCELCODEC_R12, 0x4c | ASC_MIC_INPUT);
676 /*use default value*/
677 rk1000_codec_write(codec, ACCELCODEC_R1C, ASC_DEM_ENABLE);
679 /*Select Line input*/
680 rk1000_codec_write(codec, ACCELCODEC_R12, 0x4c);
682 rk1000_codec_write(codec, ACCELCODEC_R0E, 0x10|mic_vol);
683 /*Diable route PGA->R/L Mixer, PGA gain 0db.*/
684 rk1000_codec_write(codec, ACCELCODEC_R13, 0x05 | 0 << 3);
685 rk1000_codec_write(codec, ACCELCODEC_R14, 0x05 | 0 << 3);
687 rk1000_codec_write(codec, ACCELCODEC_R04,
688 ASC_INT_MUTE_L | ASC_INT_MUTE_R |
689 ASC_SIDETONE_L_OFF | ASC_SIDETONE_R_OFF);
690 /*2set default SR and clk*/
691 rk1000_codec_write(codec, ACCELCODEC_R0A, FREQ441KHZ | ASC_NORMAL_MODE |
692 (0x10 << 1) | ASC_CLKNODIV | ASC_CLK_ENABLE);
693 g_r0_a_reg = ASC_NORMAL_MODE | (0x10 << 1) |
694 ASC_CLKNODIV | ASC_CLK_DISABLE;
695 /*2Config audio interface*/
696 rk1000_codec_write(codec, ACCELCODEC_R09, ASC_I2S_MODE |
697 ASC_16BIT_MODE | ASC_NORMAL_LRCLK |
698 ASC_LRSWAP_DISABLE | ASC_NORMAL_BCLK);
699 rk1000_codec_write(codec, ACCELCODEC_R00, ASC_HPF_ENABLE
700 | ASC_DSM_MODE_ENABLE | ASC_SCRAMBLE_ENABLE
701 | ASC_DITHER_ENABLE | ASC_BCLKDIV_4);
702 /*2volume,input,output*/
703 digital_gain = VOLUME_OUTPUT;
704 if (rk1000_codec_read(codec, ACCELCODEC_R05) != 0x0f) {
705 rk1000_codec_write(codec, ACCELCODEC_R05,
706 (digital_gain >> 8) & 0xFF);
707 rk1000_codec_write(codec, ACCELCODEC_R06, digital_gain & 0xFF);
709 if (rk1000_codec_read(codec, ACCELCODEC_R07) != 0x0f) {
710 rk1000_codec_write(codec, ACCELCODEC_R07,
711 (digital_gain >> 8) & 0xFF);
712 rk1000_codec_write(codec, ACCELCODEC_R08, digital_gain & 0xFF);
714 rk1000_codec_write(codec, ACCELCODEC_R0B,
715 ASC_DEC_ENABLE | ASC_INT_ENABLE);
716 g_r0_b_reg = ASC_DEC_ENABLE | ASC_INT_ENABLE;
717 if (rk1000_codec->boot_depop) {
719 rk1000_codec_write(codec, ACCELCODEC_R1F,
720 0x09 | ASC_PDMIXM_ENABLE);
722 rk1000_codec_write(codec, ACCELCODEC_R1F, 0x09 |
723 ASC_PDMIXM_ENABLE | ASC_PDPAM_ENABLE);
726 rk1000_codec_write(codec, ACCELCODEC_R17, VOLUME_CODEC_PA |
727 ASC_OUTPUT_ACTIVE | ASC_CROSSZERO_EN);
728 rk1000_codec_write(codec, ACCELCODEC_R18, VOLUME_CODEC_PA |
729 ASC_OUTPUT_ACTIVE | ASC_CROSSZERO_EN);
730 rk1000_codec_write(codec, ACCELCODEC_R04, ASC_INT_ACTIVE_L |
731 ASC_INT_ACTIVE_R | ASC_SIDETONE_L_OFF |
733 rk1000_codec_write(codec, ACCELCODEC_R19, 0x7F);
737 static int rk1000_codec_suspend(struct snd_soc_codec *codec)
739 DBG("Enter::%s----%d\n", __func__, __LINE__);
740 spk_ctrl_fun(GPIO_LOW);
744 static int rk1000_codec_resume(struct snd_soc_codec *codec)
746 spk_ctrl_fun(GPIO_HIGH);
750 static int rk1000_codec_probe(struct snd_soc_codec *codec)
752 struct rk1000_codec_priv *rk1000_codec;
755 rk1000_codec_codec = codec;
756 rk1000_codec = snd_soc_codec_get_drvdata(codec);
757 DBG("[%s] start\n", __func__);
758 ret = snd_soc_codec_set_cache_io(codec, 8, 16,
759 rk1000_codec->control_type);
761 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
764 /*For RK1000, i2c write&read method is special
765 *do not use system default method.*/
766 codec->write = rk1000_codec_write;
767 codec->read = rk1000_codec_read;
768 codec->hw_write = (hw_write_t)i2c_master_send;
769 if (rk1000_codec_codec == NULL) {
770 dev_err(codec->dev, "Codec device not registered\n");
773 INIT_DELAYED_WORK(&rk1000_codec->rk1000_delayed_work,
774 rk1000_delayedwork_fun);
776 if (rk1000_codec->spk_ctrl_io) {
777 ret = gpio_request(rk1000_codec->spk_ctrl_io,
780 DBG("rk1000 codec request gpio fail!\n");
783 /*set hight disable codec lr output*/
784 gpio_direction_output(rk1000_codec->spk_ctrl_io,
785 !rk1000_codec->flags);
786 gpio_set_value(rk1000_codec->spk_ctrl_io,
787 !rk1000_codec->flags);
789 rk1000_codec->call_enable = 0;
790 rk1000_codec->headset_status = HP_OUT;
791 rk1000_codec_set_bias_level(codec, SND_SOC_BIAS_PREPARE);
792 schedule_delayed_work(&rk1000_codec->rk1000_delayed_work,
793 msecs_to_jiffies(rk1000_codec->pa_enable_time));
794 rk1000_codec_reg_set();
795 DBG("rk1000_codec_probe ret=0x%x\n", ret);
799 static int rk1000_codec_remove(struct snd_soc_codec *codec)
801 rk1000_codec_set_bias_level(codec, SND_SOC_BIAS_OFF);
805 static struct snd_soc_codec_driver soc_codec_dev_rk1000_codec = {
806 .probe = rk1000_codec_probe,
807 .remove = rk1000_codec_remove,
808 .suspend = rk1000_codec_suspend,
809 .resume = rk1000_codec_resume,
810 .set_bias_level = rk1000_codec_set_bias_level,
811 .reg_cache_size = ARRAY_SIZE(rk1000_codec_reg),
812 .reg_word_size = sizeof(u16),
813 .reg_cache_default = rk1000_codec_reg,
817 static int rk1000_codec_i2c_probe(struct i2c_client *i2c,
818 const struct i2c_device_id *id)
820 struct rk1000_codec_priv *rk1000_codec;
821 struct device_node *rk1000_np = i2c->dev.of_node;
824 DBG("%s::%d\n", __func__, __LINE__);
825 rk1000_codec = kmalloc(sizeof(*rk1000_codec), GFP_KERNEL);
826 if (rk1000_codec == NULL)
828 rk1000_codec->spk_ctrl_io = of_get_named_gpio_flags(rk1000_np,
829 "spk_ctl_io", 0, &(rk1000_codec->flags));
830 if (!gpio_is_valid(rk1000_codec->spk_ctrl_io)) {
831 DBG("invalid core_info->reset_gpio: %d\n",
832 rk1000_codec->spk_ctrl_io);
835 of_property_read_u32(rk1000_np, "pa_enable_time",
836 &(rk1000_codec->pa_enable_time));
837 of_property_read_u32(rk1000_np, "boot_depop",
838 &(rk1000_codec->boot_depop));
839 i2c_set_clientdata(i2c, rk1000_codec);
840 rk1000_codec->control_type = SND_SOC_I2C;
841 ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_rk1000_codec,
843 ARRAY_SIZE(rk1000_codec_dai));
849 static int rk1000_codec_i2c_remove(struct i2c_client *client)
851 snd_soc_unregister_codec(&client->dev);
852 kfree(i2c_get_clientdata(client));
856 static const struct i2c_device_id rk1000_codec_i2c_id[] = {
857 { "rk1000_codec", 0 },
860 MODULE_DEVICE_TABLE(i2c, rk1000_codec_i2c_id);
862 /* corgi i2c codec control layer */
863 static struct i2c_driver rk1000_codec_i2c_driver = {
865 .name = "rk1000_codec",
866 .owner = THIS_MODULE,
868 .probe = rk1000_codec_i2c_probe,
869 .remove = rk1000_codec_i2c_remove,
870 .id_table = rk1000_codec_i2c_id,
874 static int __init rk1000_codec_modinit(void)
878 DBG("[%s] start\n", __func__);
879 ret = i2c_add_driver(&rk1000_codec_i2c_driver);
881 pr_err("rk1000 codec: register I2C driver err=: %d\n", ret);
884 /* late_initcall(rk1000_codec_modinit); */
885 module_init(rk1000_codec_modinit);
887 static void __exit rk1000_codec_exit(void)
889 i2c_del_driver(&rk1000_codec_i2c_driver);
891 module_exit(rk1000_codec_exit);
895 #ifdef RK1000_CODEC_PROC
896 #include <linux/proc_fs.h>
897 #include <linux/seq_file.h>
899 static int debug_write_read;
901 void rk1000_codec_reg_read(void)
903 struct snd_soc_codec *codec;
907 codec = rk1000_codec_codec;
908 for (i = 0; i <= 0x1f; i++) {
909 data = rk1000_codec_read(codec, i);
910 pr_info("reg[0x%x]=0x%x\n", i, data);
915 static ssize_t rk1000_codec_proc_write(struct file *file,
916 const char __user *buffer,
917 size_t len, loff_t *data)
925 cookie_pot = vmalloc(len);
927 pr_err("malloc cookie error for rk1000 codec proc debug\n");
930 if (copy_from_user(cookie_pot, buffer, len))
933 switch (cookie_pot[0]) {
937 debug_write_read %= 2;
938 if (debug_write_read != 0)
939 pr_info("Debug read and write reg on\n");
941 pr_info("Debug read and write reg off\n");
945 pr_info("Read reg debug\n");
946 if (cookie_pot[1] == ':') {
947 debug_write_read = 1;
948 strsep(&cookie_pot, ":");
949 while ((p = strsep(&cookie_pot, ","))) {
950 ret = kstrtol((const char *)p, 0, ®);
952 pr_err("string to long error\n");
955 value = rk1000_codec_read(rk1000_codec_codec,
957 pr_info("rk1000_codec_read:0x%04lx = 0x%04lx",
960 debug_write_read = 0;
963 pr_info("Error Read reg debug.\n");
968 pr_info("Write reg debug\n");
969 if (cookie_pot[1] == ':') {
970 debug_write_read = 1;
971 strsep(&cookie_pot, ":");
972 while ((p = strsep(&cookie_pot, "="))) {
973 ret = kstrtol(p, 0, ®);
975 pr_err("string to long error\n");
978 p = strsep(&cookie_pot, ",");
979 ret = kstrtol(p, 0, &value);
981 pr_err("string to long error\n");
984 rk1000_codec_write(rk1000_codec_codec, reg,
986 pr_info("rk1000_codec_write:0x%04lx = 0x%04lx\n",
989 debug_write_read = 0;
992 pr_info("Error Write reg debug.\n");
993 pr_info("For example: w:22=0,23=0,24=0,25=0\n");
997 rk1000_codec_reg_read();
1000 pr_info("Help for rk1000_codec_ts .\n-->The Cmd list:\n");
1001 pr_info("-->'d&&D' Open or Off the debug\n");
1002 pr_info("-->'r&&R' Read reg debug,Example:");
1003 pr_info("echo 'r:22,23,24,25'>rk1000_codec_ts\n");
1004 pr_info("-->'w&&W' Write reg debug,Example:");
1005 pr_info("echo 'w:22=0,23=0,24=0,25=0'>rk1000_codec_ts\n");
1013 static const struct file_operations rk1000_codec_proc_fops = {
1014 .owner = THIS_MODULE,
1015 .write = rk1000_codec_proc_write,
1018 static int rk1000_codec_proc_init(void)
1020 proc_create("rk1000_codec_reg", 0,
1021 NULL, &rk1000_codec_proc_fops);
1024 late_initcall(rk1000_codec_proc_init);