2 * es8323.c -- es8323 ALSA SoC audio driver
4 * Copyright 2009 Wolfson Microelectronics plc
5 * Copyright 2005 Openedhand Ltd.
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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/spi/spi.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/of_gpio.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/tlv.h>
28 #include <sound/soc.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/initval.h>
32 //#include <linux/tchip_sysinf.h>
36 #include <linux/proc_fs.h>
37 #include <linux/gpio.h>
40 #include <linux/interrupt.h>
41 #include <linux/irq.h>
44 #define DBG(x...) printk(KERN_INFO x)
46 #define DBG(x...) do { } while (0)
50 static int set_spk = 1; // add by xhc when insert hdmi 0, no insert hdmi 1
51 #define RT5633_SPK_TIMER 0 //if enable this, MUST enable RT5633_EQ_FUNC_ENA and RT5633_EQ_FUNC_SEL==RT5633_EQ_FOR_MANUAL first!
52 #if (RT5633_SPK_TIMER == 1)
53 static struct timer_list spk_timer;
54 struct work_struct spk_work;
55 static bool last_is_spk = false;
56 //#define ES8323_HP_PIN RK30_PIN0_PC7 // need modify.
58 //#define SPK_CTL RK29_PIN6_PB7
59 //#define SPK_CON RK29_PIN6_PB6
62 #ifdef CONFIG_MACH_RK_FAC
63 int es8323_hdmi_ctrl=0;
65 //#define SPK_CON RK30_PIN2_PD7 //RK30_PIN4_PC5
66 //#define HP_DET RK30_PIN0_PB5
67 #define INVALID_GPIO -1
68 int es8323_spk_con_gpio = INVALID_GPIO;
69 int es8323_hp_det_gpio = INVALID_GPIO;
71 static int hp_irq_flag = 0;
72 //#define SPK_CTL RK29_PIN6_PB6
73 //#define EAR_CON_PIN RK29_PIN6_PB5
76 #ifndef es8323_DEF_VOL
77 #define es8323_DEF_VOL 0x1e
80 static int es8323_set_bias_level(struct snd_soc_codec *codec,enum snd_soc_bias_level level);
81 extern int es8323_dapm_pre_event(struct snd_soc_dapm_widget* widget, struct snd_kcontrol * null, int event);
82 extern int es8323_dapm_post_event(struct snd_soc_dapm_widget* widget, struct snd_kcontrol * null, int event);
84 * es8323 register cache
85 * We can't read the es8323 register space when we
86 * are using 2 wire for device control, so we cache them instead.
88 static u16 es8323_reg[] = {
89 0x06, 0x1C, 0xC3, 0xFC, /* 0 *////0x0100 0x0180
90 0xC0, 0x00, 0x00, 0x7C, /* 4 */
91 0x80, 0x00, 0x00, 0x06, /* 8 */
92 0x00, 0x06, 0x30, 0x30, /* 12 */
93 0xC0, 0xC0, 0x38, 0xB0, /* 16 */
94 0x32, 0x06, 0x00, 0x00, /* 20 */
95 0x06, 0x30, 0xC0, 0xC0, /* 24 */
96 0x08, 0x06, 0x1F, 0xF7, /* 28 */
97 0xFD, 0xFF, 0x1F, 0xF7, /* 32 */
98 0xFD, 0xFF, 0x00, 0x38, /* 36 */
99 0x38, 0x38, 0x38, 0x38, /* 40 */
100 0x38, 0x00, 0x00, 0x00, /* 44 */
101 0x00, 0x00, 0x00, 0x00, /* 48 */
102 0x00, 0x00, 0x00, 0x00, /* 52 */
105 /* codec private data */
108 enum snd_soc_control_type control_type;
109 struct snd_pcm_hw_constraint_list *sysclk_constraints;
110 int is_startup; // gModify.Add
114 static void hp_detect_do_switch(struct work_struct *work)
117 int irq = gpio_to_irq(es8323_hp_det_gpio);
120 //rk28_send_wakeup_key();
121 printk("hjc:%s,irq=%d\n",__func__,irq);
122 type = gpio_get_value(es8323_hp_det_gpio) ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
123 ret = irq_set_irq_type(irq, type);
125 pr_err("%s: irq_set_irq_type(%d, %d) failed\n", __func__, irq, type);
130 if(0 == gpio_get_value(es8323_hp_det_gpio)){
131 printk("hp_det = 0,insert hp\n");
132 gpio_set_value(es8323_spk_con_gpio,0);
133 }else if(1 == gpio_get_value(es8323_hp_det_gpio)){
134 printk("hp_det = 1,deinsert hp\n");
135 gpio_set_value(es8323_spk_con_gpio,1);
141 static DECLARE_DELAYED_WORK(wakeup_work, hp_detect_do_switch);
144 static irqreturn_t hp_det_irq_handler(int irq, void *dev_id)
147 printk("%s=%d,%d\n",__FUNCTION__,HP_IRQ,HP_DET);
148 //disable_irq_nosync(ts->client->irq);
149 //queue_work(gt801_wq, &ts->work);
150 if(0 == gpio_get_value(HP_DET)){
151 printk("hp_det = 0,insert hp\n");
152 gpio_set_value(SPK_CON,0);
153 }else if(1 == gpio_get_value(HP_DET)){
154 printk("hp_det = 1,insert hp\n");
155 gpio_set_value(SPK_CON,1);
159 printk("hjc:%s>>>>\n",__func__);
160 disable_irq_nosync(irq); // for irq debounce
161 //wake_lock_timeout(&usb_wakelock, WAKE_LOCK_TIMEOUT);
162 schedule_delayed_work(&wakeup_work, HZ / 10);
169 static unsigned int es8323_read_reg_cache(struct snd_soc_codec *codec,
172 //u16 *cache = codec->reg_cache;
173 if (reg >= ARRAY_SIZE(es8323_reg))
175 return es8323_reg[reg];
178 static int es8323_write(struct snd_soc_codec *codec, unsigned int reg,
181 //u16 *cache = codec->reg_cache;
185 BUG_ON(codec->volatile_register);
188 data[1] = value & 0x00ff;
190 if (reg < ARRAY_SIZE(es8323_reg))
191 es8323_reg[reg] = value;
192 ret = codec->hw_write(codec->control_data, data, 2);
201 //#define es8323_reset(c) snd_soc_write(c, es8323_RESET, 0)
202 static int es8323_reset(struct snd_soc_codec *codec)
204 snd_soc_write(codec, ES8323_CONTROL1, 0x80);
205 return snd_soc_write(codec, ES8323_CONTROL1, 0x00);
208 static const char *es8323_line_texts[] = {
209 "Line 1", "Line 2", "PGA"};
211 static const unsigned int es8323_line_values[] = {
213 static const char *es8323_pga_sel[] = {"Line 1", "Line 2", "Differential"};
214 static const char *stereo_3d_txt[] = {"No 3D ", "Level 1","Level 2","Level 3","Level 4","Level 5","Level 6","Level 7"};
215 static const char *alc_func_txt[] = {"Off", "Right", "Left", "Stereo"};
216 static const char *ng_type_txt[] = {"Constant PGA Gain","Mute ADC Output"};
217 static const char *deemph_txt[] = {"None", "32Khz", "44.1Khz", "48Khz"};
218 static const char *adcpol_txt[] = {"Normal", "L Invert", "R Invert","L + R Invert"};
219 static const char *es8323_mono_mux[] = {"Stereo", "Mono (Left)","Mono (Right)"};
220 static const char *es8323_diff_sel[] = {"Line 1", "Line 2"};
222 static const struct soc_enum es8323_enum[]={
223 SOC_VALUE_ENUM_SINGLE(ES8323_DACCONTROL16, 3, 7, ARRAY_SIZE(es8323_line_texts), es8323_line_texts, es8323_line_values),/* LLINE */
224 SOC_VALUE_ENUM_SINGLE(ES8323_DACCONTROL16, 0, 7, ARRAY_SIZE(es8323_line_texts), es8323_line_texts, es8323_line_values),/* rline */
225 SOC_VALUE_ENUM_SINGLE(ES8323_ADCCONTROL2, 6, 3, ARRAY_SIZE(es8323_pga_sel), es8323_line_texts, es8323_line_values),/* Left PGA Mux */
226 SOC_VALUE_ENUM_SINGLE(ES8323_ADCCONTROL2, 4, 3, ARRAY_SIZE(es8323_pga_sel), es8323_line_texts, es8323_line_values),/* Right PGA Mux */
227 SOC_ENUM_SINGLE(ES8323_DACCONTROL7, 2, 8, stereo_3d_txt),/* stereo-3d */
228 SOC_ENUM_SINGLE(ES8323_ADCCONTROL10, 6, 4, alc_func_txt),/*alc func*/
229 SOC_ENUM_SINGLE(ES8323_ADCCONTROL14, 1, 2, ng_type_txt),/*noise gate type*/
230 SOC_ENUM_SINGLE(ES8323_DACCONTROL6, 6, 4, deemph_txt),/*Playback De-emphasis*/
231 SOC_ENUM_SINGLE(ES8323_ADCCONTROL6, 6, 4, adcpol_txt),
232 SOC_ENUM_SINGLE(ES8323_ADCCONTROL3, 3, 3, es8323_mono_mux),
233 SOC_ENUM_SINGLE(ES8323_ADCCONTROL3, 7, 2, es8323_diff_sel),
237 static const DECLARE_TLV_DB_SCALE(pga_tlv, 0, 300, 0);
238 static const DECLARE_TLV_DB_SCALE(adc_tlv, -9600, 50, 1);
239 static const DECLARE_TLV_DB_SCALE(dac_tlv, -9600, 50, 1);
240 static const DECLARE_TLV_DB_SCALE(out_tlv, -4500, 150, 0);
241 static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
243 static const struct snd_kcontrol_new es8323_snd_controls[] = {
244 SOC_ENUM("3D Mode", es8323_enum[4]),
245 SOC_SINGLE("ALC Capture Target Volume", ES8323_ADCCONTROL11, 4, 15, 0),
246 SOC_SINGLE("ALC Capture Max PGA", ES8323_ADCCONTROL10, 3, 7, 0),
247 SOC_SINGLE("ALC Capture Min PGA", ES8323_ADCCONTROL10, 0, 7, 0),
248 SOC_ENUM("ALC Capture Function", es8323_enum[5]),
249 SOC_SINGLE("ALC Capture ZC Switch", ES8323_ADCCONTROL13, 6, 1, 0),
250 SOC_SINGLE("ALC Capture Hold Time", ES8323_ADCCONTROL11, 0, 15, 0),
251 SOC_SINGLE("ALC Capture Decay Time", ES8323_ADCCONTROL12, 4, 15, 0),
252 SOC_SINGLE("ALC Capture Attack Time", ES8323_ADCCONTROL12, 0, 15, 0),
253 SOC_SINGLE("ALC Capture NG Threshold", ES8323_ADCCONTROL14, 3, 31, 0),
254 SOC_ENUM("ALC Capture NG Type",es8323_enum[6]),
255 SOC_SINGLE("ALC Capture NG Switch", ES8323_ADCCONTROL14, 0, 1, 0),
256 SOC_SINGLE("ZC Timeout Switch", ES8323_ADCCONTROL13, 6, 1, 0),
257 SOC_DOUBLE_R_TLV("Capture Digital Volume", ES8323_ADCCONTROL8, ES8323_ADCCONTROL9,0, 255, 1, adc_tlv),
258 SOC_SINGLE("Capture Mute", ES8323_ADCCONTROL7, 2, 1, 0),
259 SOC_SINGLE_TLV("Left Channel Capture Volume", ES8323_ADCCONTROL1, 4, 15, 0, bypass_tlv),
260 SOC_SINGLE_TLV("Right Channel Capture Volume", ES8323_ADCCONTROL1, 0, 15, 0, bypass_tlv),
261 SOC_ENUM("Playback De-emphasis", es8323_enum[7]),
262 SOC_ENUM("Capture Polarity", es8323_enum[8]),
263 SOC_DOUBLE_R_TLV("PCM Volume", ES8323_DACCONTROL4, ES8323_DACCONTROL5, 0, 255, 1, dac_tlv),
264 SOC_SINGLE_TLV("Left Mixer Left Bypass Volume", ES8323_DACCONTROL17, 3, 7, 1, bypass_tlv),
265 SOC_SINGLE_TLV("Right Mixer Right Bypass Volume", ES8323_DACCONTROL20, 3, 7, 1, bypass_tlv),
266 SOC_DOUBLE_R_TLV("Output 1 Playback Volume", ES8323_DACCONTROL24, ES8323_DACCONTROL25, 0, 64, 0, out_tlv),
267 SOC_DOUBLE_R_TLV("Output 2 Playback Volume", ES8323_DACCONTROL26, ES8323_DACCONTROL27, 0, 64, 0, out_tlv),
271 static const struct snd_kcontrol_new es8323_left_line_controls =
272 SOC_DAPM_VALUE_ENUM("Route", es8323_enum[0]);
274 static const struct snd_kcontrol_new es8323_right_line_controls =
275 SOC_DAPM_VALUE_ENUM("Route", es8323_enum[1]);
278 static const struct snd_kcontrol_new es8323_left_pga_controls =
279 SOC_DAPM_VALUE_ENUM("Route", es8323_enum[2]);
281 static const struct snd_kcontrol_new es8323_right_pga_controls =
282 SOC_DAPM_VALUE_ENUM("Route", es8323_enum[3]);
285 static const struct snd_kcontrol_new es8323_left_mixer_controls[] = {
286 SOC_DAPM_SINGLE("Left Playback Switch", ES8323_DACCONTROL17, 7, 1, 0),
287 SOC_DAPM_SINGLE("Left Bypass Switch", ES8323_DACCONTROL17, 6, 1, 0),
291 static const struct snd_kcontrol_new es8323_right_mixer_controls[] = {
292 SOC_DAPM_SINGLE("Right Playback Switch", ES8323_DACCONTROL20, 7, 1, 0),
293 SOC_DAPM_SINGLE("Right Bypass Switch", ES8323_DACCONTROL20, 6, 1, 0),
296 /* Differential Mux */
297 //static const char *es8323_diff_sel[] = {"Line 1", "Line 2"};
298 static const struct snd_kcontrol_new es8323_diffmux_controls =
299 SOC_DAPM_ENUM("Route", es8323_enum[10]);
302 static const struct snd_kcontrol_new es8323_monomux_controls =
303 SOC_DAPM_ENUM("Route", es8323_enum[9]);
305 static const struct snd_soc_dapm_widget es8323_dapm_widgets[] = {
307 SND_SOC_DAPM_INPUT("LINPUT1"),
308 SND_SOC_DAPM_INPUT("LINPUT2"),
309 SND_SOC_DAPM_INPUT("RINPUT1"),
310 SND_SOC_DAPM_INPUT("RINPUT2"),
312 SND_SOC_DAPM_MICBIAS("Mic Bias", ES8323_ADCPOWER, 3, 1),
314 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
315 &es8323_diffmux_controls),
317 SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
318 &es8323_monomux_controls),
319 SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
320 &es8323_monomux_controls),
322 SND_SOC_DAPM_MUX("Left PGA Mux", ES8323_ADCPOWER, 7, 1,
323 &es8323_left_pga_controls),
324 SND_SOC_DAPM_MUX("Right PGA Mux", ES8323_ADCPOWER, 6, 1,
325 &es8323_right_pga_controls),
327 SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
328 &es8323_left_line_controls),
329 SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
330 &es8323_right_line_controls),
332 SND_SOC_DAPM_ADC("Right ADC", "Right Capture", ES8323_ADCPOWER, 4, 1),
333 SND_SOC_DAPM_ADC("Left ADC", "Left Capture", ES8323_ADCPOWER, 5, 1),
335 /* gModify.Cmmt Implement when suspend/startup */
336 SND_SOC_DAPM_DAC("Right DAC", "Right Playback", ES8323_DACPOWER, 7, 0),
337 SND_SOC_DAPM_DAC("Left DAC", "Left Playback", ES8323_DACPOWER, 8, 0),
339 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
340 &es8323_left_mixer_controls[0],
341 ARRAY_SIZE(es8323_left_mixer_controls)),
342 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
343 &es8323_right_mixer_controls[0],
344 ARRAY_SIZE(es8323_right_mixer_controls)),
346 SND_SOC_DAPM_PGA("Right Out 2", ES8323_DACPOWER, 2, 0, NULL, 0),
347 SND_SOC_DAPM_PGA("Left Out 2", ES8323_DACPOWER, 3, 0, NULL, 0),
348 SND_SOC_DAPM_PGA("Right Out 1", ES8323_DACPOWER, 4, 0, NULL, 0),
349 SND_SOC_DAPM_PGA("Left Out 1", ES8323_DACPOWER, 5, 0, NULL, 0),
350 SND_SOC_DAPM_PGA("LAMP", ES8323_ADCCONTROL1, 4, 0, NULL, 0),
351 SND_SOC_DAPM_PGA("RAMP", ES8323_ADCCONTROL1, 0, 0, NULL, 0),
353 SND_SOC_DAPM_OUTPUT("LOUT1"),
354 SND_SOC_DAPM_OUTPUT("ROUT1"),
355 SND_SOC_DAPM_OUTPUT("LOUT2"),
356 SND_SOC_DAPM_OUTPUT("ROUT2"),
357 SND_SOC_DAPM_OUTPUT("VREF"),
359 SND_SOC_DAPM_PRE("PRE", es8323_dapm_pre_event),
360 SND_SOC_DAPM_POST("POST", es8323_dapm_post_event),
364 static const struct snd_soc_dapm_route audio_map[] = {
366 { "Left Line Mux", "NULL", "LINPUT1" },
367 { "Left Line Mux", "NULL", "LINPUT2" },
368 { "Left Line Mux", "NULL", "Left PGA Mux" },
370 { "Right Line Mux", "NULL", "RINPUT1" },
371 { "Right Line Mux", "NULL", "RINPUT2" },
372 { "Right Line Mux", "NULL", "Right PGA Mux" },
374 { "Left PGA Mux", "LAMP", "LINPUT1" },
375 { "Left PGA Mux", "LAMP", "LINPUT2" },
376 { "Left PGA Mux", "LAMP", "Differential Mux" },
378 { "Right PGA Mux", "RAMP", "RINPUT1" },
379 { "Right PGA Mux", "RAMP", "RINPUT2" },
380 { "Right PGA Mux", "RAMP", "Differential Mux" },
382 { "Differential Mux", "LAMP", "LINPUT1" },
383 { "Differential Mux", "RAMP", "RINPUT1" },
384 { "Differential Mux", "LAMP", "LINPUT2" },
385 { "Differential Mux", "RAMP", "RINPUT2" },
387 { "Left ADC Mux", "Stereo", "Left PGA Mux" },
388 { "Left ADC Mux", "Mono (Left)", "Left PGA Mux" },
389 //{ "Left ADC Mux", "Digital Mono", "Left PGA Mux" },
391 { "Right ADC Mux", "Stereo", "Right PGA Mux" },
392 { "Right ADC Mux", "Mono (Right)", "Right PGA Mux" },
393 //{ "Right ADC Mux", "Digital Mono", "Right PGA Mux" },
395 { "Left ADC", NULL, "Left ADC Mux" },
396 { "Right ADC", NULL, "Right ADC Mux" },
398 { "Left Line Mux", "LAMP", "LINPUT1" },
399 { "Left Line Mux", "LAMP", "LINPUT2" },
400 { "Left Line Mux", "LAMP", "Left PGA Mux" },
402 { "Right Line Mux", "RAMP", "RINPUT1" },
403 { "Right Line Mux", "RAMP", "RINPUT2" },
404 { "Right Line Mux", "RAMP", "Right PGA Mux" },
406 { "Left Mixer", "Left Playback Switch", "Left DAC" },
407 { "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
409 { "Right Mixer", "Right Playback Switch", "Right DAC" },
410 { "Right Mixer", "Right Bypass Switch", "Right Line Mux" },
412 { "Left Out 1", NULL, "Left Mixer" },
413 { "LOUT1", NULL, "Left Out 1" },
414 { "Right Out 1", NULL, "Right Mixer" },
415 { "ROUT1", NULL, "Right Out 1" },
417 { "Left Out 2", NULL, "Left Mixer" },
418 { "LOUT2", NULL, "Left Out 2" },
419 { "Right Out 2", NULL, "Right Mixer" },
420 { "ROUT2", NULL, "Right Out 2" },
423 int es8323_dapm_pre_event(struct snd_soc_dapm_widget* widget, struct snd_kcontrol * null, int event)
425 // printk("fun:%s, event:%d\r\n", __FUNCTION__, event);
428 //widget->dapm->dev_power = 1;
429 es8323_set_bias_level(widget->codec, SND_SOC_BIAS_PREPARE);
433 int es8323_dapm_post_event(struct snd_soc_dapm_widget* widget, struct snd_kcontrol * null, int event)
435 // printk("fun:%s, event:%d\r\n", __FUNCTION__, event);
438 //widget->dapm->dev_power = 0;
439 es8323_set_bias_level(widget->codec, SND_SOC_BIAS_STANDBY);
452 /* codec hifi mclk clock divider coefficients */
453 static const struct _coeff_div coeff_div[] = {
455 {12288000, 8000, 1536, 0xa, 0x0},
456 {11289600, 8000, 1408, 0x9, 0x0},
457 {18432000, 8000, 2304, 0xc, 0x0},
458 {16934400, 8000, 2112, 0xb, 0x0},
459 {12000000, 8000, 1500, 0xb, 0x1},
462 {11289600, 11025, 1024, 0x7, 0x0},
463 {16934400, 11025, 1536, 0xa, 0x0},
464 {12000000, 11025, 1088, 0x9, 0x1},
467 {12288000, 16000, 768, 0x6, 0x0},
468 {18432000, 16000, 1152, 0x8, 0x0},
469 {12000000, 16000, 750, 0x7, 0x1},
472 {11289600, 22050, 512, 0x4, 0x0},
473 {16934400, 22050, 768, 0x6, 0x0},
474 {12000000, 22050, 544, 0x6, 0x1},
477 {12288000, 32000, 384, 0x3, 0x0},
478 {18432000, 32000, 576, 0x5, 0x0},
479 {12000000, 32000, 375, 0x4, 0x1},
482 {11289600, 44100, 256, 0x2, 0x0},
483 {16934400, 44100, 384, 0x3, 0x0},
484 {12000000, 44100, 272, 0x3, 0x1},
487 {12288000, 48000, 256, 0x2, 0x0},
488 {18432000, 48000, 384, 0x3, 0x0},
489 {12000000, 48000, 250, 0x2, 0x1},
492 {11289600, 88200, 128, 0x0, 0x0},
493 {16934400, 88200, 192, 0x1, 0x0},
494 {12000000, 88200, 136, 0x1, 0x1},
497 {12288000, 96000, 128, 0x0, 0x0},
498 {18432000, 96000, 192, 0x1, 0x0},
499 {12000000, 96000, 125, 0x0, 0x1},
502 static inline int get_coeff(int mclk, int rate)
506 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
507 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
514 /* The set of rates we can generate from the above for each SYSCLK */
516 static unsigned int rates_12288[] = {
517 8000, 12000, 16000, 24000, 24000, 32000, 48000, 96000,
520 static struct snd_pcm_hw_constraint_list constraints_12288 = {
521 .count = ARRAY_SIZE(rates_12288),
525 static unsigned int rates_112896[] = {
526 8000, 11025, 22050, 44100,
529 static struct snd_pcm_hw_constraint_list constraints_112896 = {
530 .count = ARRAY_SIZE(rates_112896),
531 .list = rates_112896,
534 static unsigned int rates_12[] = {
535 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
539 static struct snd_pcm_hw_constraint_list constraints_12 = {
540 .count = ARRAY_SIZE(rates_12),
544 static void on_off_ext_amp(int i)
546 // struct snd_soc_codec *codec;
551 gpio_set_value(es8323_spk_con_gpio, i); //delete by hjc
553 DBG("*** %s() SPEAKER set SPK_CON %d\n", __FUNCTION__, i);
556 //gpio_direction_output(SPK_CTL, GPIO_LOW);
557 gpio_set_value(SPK_CTL, i);
558 DBG("*** %s() SPEAKER set as %d\n", __FUNCTION__, i);
561 //gpio_direction_output(EAR_CON_PIN, GPIO_LOW);
562 gpio_set_value(EAR_CON_PIN, i);
563 DBG("*** %s() HEADPHONE set as %d\n", __FUNCTION__, i);
569 void es8323_codec_set_spk(bool on)
578 * Note that this should be called from init rather than from hw_params.
580 static int es8323_set_dai_sysclk(struct snd_soc_dai *codec_dai,
581 int clk_id, unsigned int freq, int dir)
583 struct snd_soc_codec *codec = codec_dai->codec;
584 struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
586 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
593 es8323->sysclk_constraints = &constraints_112896;
594 es8323->sysclk = freq;
601 es8323->sysclk_constraints = &constraints_12288;
602 es8323->sysclk = freq;
607 es8323->sysclk_constraints = &constraints_12;
608 es8323->sysclk = freq;
614 static int es8323_set_dai_fmt(struct snd_soc_dai *codec_dai,
617 struct snd_soc_codec *codec = codec_dai->codec;
621 alsa_dbg("%s----%d, fmt[%02x]\n",__FUNCTION__,__LINE__,fmt);
623 iface = snd_soc_read(codec, ES8323_IFACE);
624 adciface = snd_soc_read(codec, ES8323_ADC_IFACE);
625 daciface = snd_soc_read(codec, ES8323_DAC_IFACE);
627 /* set master/slave audio interface */
628 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
629 case SND_SOC_DAIFMT_CBM_CFM: // MASTER MODE
630 alsa_dbg("es8323 in master mode");
633 case SND_SOC_DAIFMT_CBS_CFS: // SLAVE MODE
634 alsa_dbg("es8323 in slave mode");
642 /* interface format */
643 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
644 case SND_SOC_DAIFMT_I2S:
646 //daciface &= 0xF9; //updated by david-everest,5-25
649 case SND_SOC_DAIFMT_RIGHT_J:
651 case SND_SOC_DAIFMT_LEFT_J:
653 case SND_SOC_DAIFMT_DSP_A:
655 case SND_SOC_DAIFMT_DSP_B:
661 /* clock inversion */
662 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
663 case SND_SOC_DAIFMT_NB_NF:
666 //daciface &= 0xDF; //UPDATED BY david-everest,5-25
669 case SND_SOC_DAIFMT_IB_IF:
671 //adciface &= 0xDF; //UPDATED BY david-everest,5-25
673 //daciface &= 0xDF; //UPDATED BY david-everest,5-25
676 case SND_SOC_DAIFMT_IB_NF:
678 // adciface |= 0x40; //UPDATED BY david-everest,5-25
680 //daciface |= 0x40; //UPDATED BY david-everest,5-25
683 case SND_SOC_DAIFMT_NB_IF:
686 //daciface |= 0x20; //UPDATED BY david-everest,5-25
693 snd_soc_write(codec, ES8323_IFACE , iface);
694 snd_soc_write(codec, ES8323_ADC_IFACE, adciface);
695 snd_soc_write(codec, ES8323_DAC_IFACE, daciface);
700 static int es8323_pcm_startup(struct snd_pcm_substream *substream,
701 struct snd_soc_dai *dai)
703 struct snd_soc_codec *codec = dai->codec;
704 struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
706 if (!es8323->is_startup) {
707 es8323->is_startup = 1;
710 snd_soc_write(codec, ES8323_CONTROL1, 0x06);
711 snd_soc_write(codec, ES8323_CONTROL2, 0x72);
712 snd_soc_write(codec, ES8323_DACPOWER, 0x00);
714 //snd_soc_write(codec, ES8323_CHIPPOWER, 0xf3);
715 snd_soc_write(codec, ES8323_DACCONTROL21, 0x80);
717 snd_soc_write(codec, ES8323_ADCPOWER, 0x59);
718 snd_soc_write(codec, ES8323_DACPOWER, 0x3c);
719 snd_soc_write(codec, ES8323_CHIPPOWER, 0x00);
723 DBG("Enter::%s----%d es8323->sysclk=%d\n",__FUNCTION__,__LINE__,es8323->sysclk);
725 /* The set of sample rates that can be supported depends on the
726 * MCLK supplied to the CODEC - enforce this.
728 if (!es8323->sysclk) {
730 "No MCLK configured, call set_sysclk() on init\n");
734 snd_pcm_hw_constraint_list(substream->runtime, 0,
735 SNDRV_PCM_HW_PARAM_RATE,
736 es8323->sysclk_constraints);
741 static int es8323_pcm_hw_params(struct snd_pcm_substream *substream,
742 struct snd_pcm_hw_params *params,
743 struct snd_soc_dai *dai)
746 static int codecfirstuse=0;
747 struct snd_soc_pcm_runtime *rtd = substream->private_data;
748 struct snd_soc_codec *codec = rtd->codec;
749 struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
750 //u16 iface = snd_soc_read(codec, es8323_IFACE) & 0x1f3;
751 //u16 srate = snd_soc_read(codec, es8323_SRATE) & 0x180;
753 u16 srate = snd_soc_read(codec, ES8323_IFACE) & 0x80;
754 u16 adciface = snd_soc_read(codec, ES8323_ADC_IFACE) & 0xE3;
755 u16 daciface = snd_soc_read(codec, ES8323_DAC_IFACE) & 0xC7;
759 coeff = get_coeff(es8323->sysclk, params_rate(params));
761 coeff = get_coeff(es8323->sysclk / 2, params_rate(params));
766 "Unable to configure sample rate %dHz with %dHz MCLK\n",
767 params_rate(params), es8323->sysclk);
772 switch (params_format(params)) {
773 case SNDRV_PCM_FORMAT_S16_LE:
777 case SNDRV_PCM_FORMAT_S20_3LE:
781 case SNDRV_PCM_FORMAT_S24_LE:
783 case SNDRV_PCM_FORMAT_S32_LE:
789 /* set iface & srate*/
790 snd_soc_write(codec, ES8323_DAC_IFACE, daciface); //dac bits length
791 snd_soc_write(codec, ES8323_ADC_IFACE, adciface); //adc bits length
795 snd_soc_write(codec, ES8323_IFACE, srate); //bclk div,mclkdiv2
796 snd_soc_write(codec, ES8323_ADCCONTROL5, coeff_div[coeff].sr | (coeff_div[coeff].usb) << 4);
797 snd_soc_write(codec, ES8323_DACCONTROL2, coeff_div[coeff].sr | (coeff_div[coeff].usb) << 4);
799 if (codecfirstuse == 0)
801 snd_soc_write(codec, ES8323_LOUT2_VOL, es8323_DEF_VOL);//0x1c); //
802 snd_soc_write(codec, ES8323_ROUT2_VOL, es8323_DEF_VOL);//0x1c); //
809 static int es8323_mute(struct snd_soc_dai *dai, int mute)
811 struct snd_soc_codec *codec = dai->codec;
812 // u16 mute_reg = snd_soc_read(codec, ES8323_DACCONTROL3) & 0xfb;
814 DBG("Enter::%s----%d--mute=%d\n",__FUNCTION__,__LINE__,mute);
817 //snd_soc_write(codec, ES8323_DACCONTROL3, mute_reg | 0x4);
819 snd_soc_write(codec, ES8323_DACCONTROL3, 0x06);//0xe6);
825 snd_soc_write(codec, ES8323_DACCONTROL3, 0x02);//0xe2);
826 snd_soc_write(codec, 0x30,0x1e);
827 snd_soc_write(codec, 0x31,0x1e);
831 on_off_ext_amp(!mute);
836 /////////////////////////////////////////////////////////////////
837 static int es8323_set_bias_level(struct snd_soc_codec *codec,
838 enum snd_soc_bias_level level)
840 struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
841 // u16 OUT_VOL = snd_soc_read(codec, ES8323_LOUT1_VOL);
844 DBG("Enter::%s----%d level =%d\n",__FUNCTION__,__LINE__,level);
846 case SND_SOC_BIAS_ON:
847 es8323->is_biason = 1;
849 case SND_SOC_BIAS_PREPARE:
850 snd_soc_write(codec, ES8323_ANAVOLMANAG, 0x7C);
851 snd_soc_write(codec, ES8323_CHIPLOPOW1, 0x00);
852 snd_soc_write(codec, ES8323_CHIPLOPOW2, 0xFF);
853 snd_soc_write(codec, ES8323_CHIPPOWER, 0x00);
854 snd_soc_write(codec, ES8323_ADCPOWER, 0x59);
856 case SND_SOC_BIAS_STANDBY:
857 snd_soc_write(codec, ES8323_ANAVOLMANAG, 0x7C);
858 snd_soc_write(codec, ES8323_CHIPLOPOW1, 0x00);
859 snd_soc_write(codec, ES8323_CHIPLOPOW2, 0xFF);
860 snd_soc_write(codec, ES8323_CHIPPOWER, 0x00);
861 snd_soc_write(codec, ES8323_ADCPOWER, 0x59);
863 case SND_SOC_BIAS_OFF:
864 snd_soc_write(codec, ES8323_ANAVOLMANAG, 0x7B);
865 snd_soc_write(codec, ES8323_CHIPLOPOW1, 0xFF);
866 snd_soc_write(codec, ES8323_CHIPLOPOW2, 0xFF);
867 snd_soc_write(codec, ES8323_ADCPOWER, 0xFF);
868 snd_soc_write(codec, ES8323_CHIPPOWER, 0xAA);
870 //snd_soc_write(codec, 0x2b, 0x90);
873 codec->dapm.bias_level = level;
879 #define es8323_RATES SNDRV_PCM_RATE_8000_96000
881 #define es8323_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
882 SNDRV_PCM_FMTBIT_S24_LE)
884 static struct snd_soc_dai_ops es8323_ops = {
885 .startup = es8323_pcm_startup,
886 .hw_params = es8323_pcm_hw_params,
887 .set_fmt = es8323_set_dai_fmt,
888 .set_sysclk = es8323_set_dai_sysclk,
889 .digital_mute = es8323_mute,
892 static struct snd_soc_dai_driver es8323_dai = {
893 .name = "ES8323 HiFi",
895 .stream_name = "Playback",
898 .rates = es8323_RATES,
899 .formats = es8323_FORMATS,
902 .stream_name = "Capture",
905 .rates = es8323_RATES,
906 .formats = es8323_FORMATS,
909 .symmetric_rates = 1,
912 static int es8323_suspend(struct snd_soc_codec *codec)
915 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
918 snd_soc_write(codec, 0x19, 0x06);
919 snd_soc_write(codec, 0x07, 0x7B);
920 snd_soc_write(codec, 0x06, 0xFF);
921 snd_soc_write(codec, 0x05, 0xFF);
924 snd_soc_write(codec, 0x19, 0x06);
925 snd_soc_write(codec, 0x30, 0x00);
926 snd_soc_write(codec, 0x31, 0x00);
927 snd_soc_write(codec, ES8323_ADCPOWER, 0xFF);
928 snd_soc_write(codec, ES8323_DACPOWER, 0xc0);
929 snd_soc_write(codec, ES8323_CHIPPOWER, 0xF3);
930 snd_soc_write(codec, 0x00, 0x00);
931 snd_soc_write(codec, 0x01, 0x58);
932 snd_soc_write(codec, 0x2b, 0x9c);
934 gpio_set_value(es8323_spk_con_gpio, 0);
938 static int es8323_resume(struct snd_soc_codec *codec)
942 // u16 *cache = codec->reg_cache;
943 snd_soc_write(codec, 0x2b, 0x80);
944 snd_soc_write(codec, 0x01, 0x50);
945 snd_soc_write(codec, 0x00, 0x32);
946 snd_soc_write(codec, ES8323_CHIPPOWER, 0x00);
947 snd_soc_write(codec, ES8323_DACPOWER, 0x0c);
948 snd_soc_write(codec, ES8323_ADCPOWER, 0x59);
949 snd_soc_write(codec, 0x31, es8323_DEF_VOL);
950 snd_soc_write(codec, 0x30, es8323_DEF_VOL);
951 snd_soc_write(codec, 0x19, 0x02);
952 gpio_set_value(es8323_spk_con_gpio, 1);
956 static u32 cur_reg=0;
957 static struct snd_soc_codec *es8323_codec;
958 static int entry_read(char *page, char **start, off_t off,
959 int count, int *eof, void *data)
963 snd_soc_write(es8323_codec, ES8323_ADCPOWER, 0xff);
964 snd_soc_write(es8323_codec, ES8323_DACPOWER, 0xf0);
965 snd_soc_write(es8323_codec, ES8323_DACPOWER, 0xc0);
966 snd_soc_write(es8323_codec, ES8323_CHIPPOWER, 0xf3);
968 len = sprintf(page, "es8323 suspend...\n");
973 #if (RT5633_SPK_TIMER == 1)
974 static void spk_work_handler(struct work_struct *work)
976 //if(!gpio_get_value(ES8323_HP_PIN)){
977 //gpio_direction_output(SPK_CON,0);
978 // gpio_set_value(SPK_CON, 0);
980 //gpio_direction_output(SPK_CON,1);
981 // gpio_set_value(SPK_CON, 1);
984 void spk_timer_callback(unsigned long data )
987 schedule_work(&spk_work);
988 ret = mod_timer(&spk_timer, jiffies + msecs_to_jiffies(1000));
989 if (ret) printk("Error in mod_timer\n");
993 static int es8323_probe(struct snd_soc_codec *codec)
995 // struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
996 struct snd_soc_dapm_context *dapm = &codec->dapm;
998 unsigned long flags=0;
1001 printk("%s\n", __func__);
1003 ret = gpio_request(RK30_PIN0_PC7, NULL);
1005 printk("%s request RK30_PIN0_PC7 error", __func__);
1008 gpio_direction_input(RK30_PIN0_PC7);
1010 ret = gpio_request(es8323_spk_con_gpio, NULL);
1012 printk("%s request SPK_CON error", __func__);
1015 //gpio_set_value(SPK_CON, 1);
1016 gpio_direction_output(es8323_spk_con_gpio,0);
1019 ret = gpio_request(es8323_hp_det_gpio, NULL);
1021 printk("%s request HP_DET error", __func__);
1024 gpio_direction_input(es8323_hp_det_gpio);
1026 flags = gpio_get_value(es8323_hp_det_gpio) ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1028 HP_IRQ = gpio_to_irq(es8323_hp_det_gpio);
1030 ret = request_irq(HP_IRQ, hp_det_irq_handler, flags, "ES8323", NULL);
1032 printk("%s:register ISR (irq=%d)\n", __FUNCTION__,HP_IRQ);
1035 printk("request_irq HP_IRQ failed\n");
1038 if (codec == NULL) {
1039 dev_err(codec->dev, "Codec device not registered\n");
1042 codec->read = es8323_read_reg_cache;
1043 codec->write = es8323_write;
1044 codec->hw_write = (hw_write_t)i2c_master_send;
1045 codec->control_data = container_of(codec->dev, struct i2c_client, dev);
1047 es8323_codec = codec;
1048 ret = es8323_reset(codec);
1050 dev_err(codec->dev, "Failed to issue reset\n");
1053 #if (RT5633_SPK_TIMER == 1)
1054 setup_timer( &spk_timer, spk_timer_callback, 0 );
1055 ret = mod_timer( &spk_timer, jiffies + msecs_to_jiffies(5000) );
1057 printk("Error in mod_timer\n");
1058 INIT_WORK(&spk_work, spk_work_handler);
1062 //es8323_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1065 //snd_soc_write(codec, 0x35 , 0xa0);
1066 //snd_soc_write(codec, 0x36 , 0x08); //for 1.8V VDD
1067 snd_soc_write(codec, 0x02,0xf3);
1068 snd_soc_write(codec, 0x2B,0x80);
1069 snd_soc_write(codec, 0x08,0x00); //ES8388 salve
1070 snd_soc_write(codec, 0x00,0x32); //
1071 snd_soc_write(codec, 0x01,0x72); //PLAYBACK & RECORD Mode,EnRefr=1
1072 snd_soc_write(codec, 0x03,0x59); //pdn_ana=0,ibiasgen_pdn=0
1073 snd_soc_write(codec, 0x05,0x00); //pdn_ana=0,ibiasgen_pdn=0
1074 snd_soc_write(codec, 0x06,0xc3); //pdn_ana=0,ibiasgen_pdn=0
1075 snd_soc_write(codec, 0x09,0x88); //ADC L/R PGA = +24dB
1076 //----------------------------------------------------------------------------------------------------------------
1077 snd_soc_write(codec, 0x0a,0xf0); //ADC INPUT=LIN2/RIN2
1078 snd_soc_write(codec, 0x0b,0x82); //ADC INPUT=LIN2/RIN2 //82
1079 //-----------------------------------------------------------------------------------------------------------------
1080 snd_soc_write(codec, 0x0C,0x4c); //I2S-24BIT
1081 snd_soc_write(codec, 0x0d,0x02); //MCLK/LRCK=256
1082 snd_soc_write(codec, 0x10,0x00); //ADC Left Volume=0db
1083 snd_soc_write(codec, 0x11,0x00); //ADC Right Volume=0db
1084 snd_soc_write(codec, 0x12,0xea); // ALC stereo MAXGAIN: 35.5dB, MINGAIN: +6dB (Record Volume increased!)
1085 snd_soc_write(codec, 0x13,0xc0);
1086 snd_soc_write(codec, 0x14,0x05);
1087 snd_soc_write(codec, 0x15,0x06);
1088 snd_soc_write(codec, 0x16,0x53);
1089 snd_soc_write(codec, 0x17,0x18); //I2S-16BIT
1090 snd_soc_write(codec, 0x18,0x02);
1091 snd_soc_write(codec, 0x1A,0x00); //DAC VOLUME=0DB
1092 snd_soc_write(codec, 0x1B,0x00);
1094 snd_soc_write(codec, 0x1E,0x01); //for 47uF capacitors ,15db Bass@90Hz,Fs=44100
1095 snd_soc_write(codec, 0x1F,0x84);
1096 snd_soc_write(codec, 0x20,0xED);
1097 snd_soc_write(codec, 0x21,0xAF);
1098 snd_soc_write(codec, 0x22,0x20);
1099 snd_soc_write(codec, 0x23,0x6C);
1100 snd_soc_write(codec, 0x24,0xE9);
1101 snd_soc_write(codec, 0x25,0xBE);
1103 snd_soc_write(codec, 0x26,0x12); //Left DAC TO Left IXER
1104 snd_soc_write(codec, 0x27,0xb8); //Left DAC TO Left MIXER
1105 snd_soc_write(codec, 0x28,0x38);
1106 snd_soc_write(codec, 0x29,0x38);
1107 snd_soc_write(codec, 0x2A,0xb8);
1108 snd_soc_write(codec, 0x02,0x00); //aa //START DLL and state-machine,START DSM
1109 snd_soc_write(codec, 0x19,0x02); //SOFT RAMP RATE=32LRCKS/STEP,Enable ZERO-CROSS CHECK,DAC MUTE
1110 snd_soc_write(codec, 0x04,0x0c); //pdn_ana=0,ibiasgen_pdn=0
1112 snd_soc_write(codec, 0x2e,0x00);
1113 snd_soc_write(codec, 0x2f,0x00);
1114 snd_soc_write(codec, 0x30,0x08);
1115 snd_soc_write(codec, 0x31,0x08);
1117 snd_soc_write(codec, 0x30,0x0f);
1118 snd_soc_write(codec, 0x31,0x0f);
1120 snd_soc_write(codec, 0x30,0x18);
1121 snd_soc_write(codec, 0x31,0x18);
1123 snd_soc_write(codec, 0x04,0x2c); //pdn_ana=0,ibiasgen_pdn=0
1126 //s8323_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1127 //codec->dapm.bias_level = SND_SOC_BIAS_STANDBY;
1129 snd_soc_add_codec_controls(codec, es8323_snd_controls,
1130 ARRAY_SIZE(es8323_snd_controls));
1131 snd_soc_dapm_new_controls(dapm, es8323_dapm_widgets,
1132 ARRAY_SIZE(es8323_dapm_widgets));
1133 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
1135 //create_proc_read_entry("es8323_suspend", 0644, NULL, entry_read, NULL);
1140 static int es8323_remove(struct snd_soc_codec *codec)
1142 es8323_set_bias_level(codec, SND_SOC_BIAS_OFF);
1146 void es8323_codec_set_spk(bool on)
1148 DBG("Enter::%s----%d--, on = %d\n",__FUNCTION__,__LINE__, on);
1151 gpio_set_value(es8323_spk_con_gpio, on);
1155 static struct snd_soc_codec_driver soc_codec_dev_es8323 = {
1156 .probe = es8323_probe,
1157 .remove = es8323_remove,
1158 .suspend = es8323_suspend,
1159 .resume = es8323_resume,
1160 .set_bias_level = es8323_set_bias_level,
1161 .reg_cache_size = ARRAY_SIZE(es8323_reg),
1162 .reg_word_size = sizeof(u16),
1163 .reg_cache_default = es8323_reg,
1164 //------------------------------------------
1165 //.volatile_register = es8323_volatile_register,
1166 //.readable_register = es8323_readable_register,
1167 .reg_cache_step = 1,
1169 .controls = es8323_snd_controls,
1170 .num_controls = ARRAY_SIZE(es8323_snd_controls),
1171 .dapm_routes = audio_map,
1172 .num_dapm_routes = ARRAY_SIZE(audio_map),
1173 .dapm_widgets = es8323_dapm_widgets,
1174 .num_dapm_widgets = ARRAY_SIZE(es8323_dapm_widgets),
1176 //--------------------------------------------------
1177 .read = es8323_read_reg_cache,
1178 .write = es8323_write,
1182 #if defined(CONFIG_SPI_MASTER)
1183 static int es8323_spi_probe(struct spi_device *spi)
1185 struct es8323_priv *es8323;
1188 es8323 = kzalloc(sizeof(struct es8323_priv), GFP_KERNEL);
1192 es8323->control_type = SND_SOC_SPI;
1193 spi_set_drvdata(spi, es8323);
1195 ret = snd_soc_register_codec(&spi->dev,
1196 &soc_codec_dev_es8323, &es8323_dai, 1);
1202 static int es8323_spi_remove(struct spi_device *spi)
1204 snd_soc_unregister_codec(&spi->dev);
1205 kfree(spi_get_drvdata(spi));
1209 static struct spi_driver es8323_spi_driver = {
1212 .owner = THIS_MODULE,
1214 .probe = es8323_spi_probe,
1215 .remove = es8323_spi_remove,
1217 #endif /* CONFIG_SPI_MASTER */
1219 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1220 static ssize_t es8323_show(struct device *dev, struct device_attribute *attr, char *_buf)
1222 return sprintf(_buf, "%s(): get 0x%04x=0x%04x\n", __FUNCTION__, cur_reg,
1223 snd_soc_read(es8323_codec, cur_reg));
1226 static u32 strtol(const char *nptr, int base)
1229 if(!nptr || (base!=16 && base!=10 && base!=8))
1232 printk("%s(): NULL pointer input\n", __FUNCTION__);
1235 for(ret=0; *nptr; nptr++)
1239 if((base==16 && *nptr>='A' && *nptr<='F') ||
1240 (base==16 && *nptr>='a' && *nptr<='f') ||
1241 (base>=10 && *nptr>='0' && *nptr<='9') ||
1242 (base>=8 && *nptr>='0' && *nptr<='7') )
1245 if(base==16 && *nptr>='A' && *nptr<='F')
1246 ret += *nptr-'A'+10;
1247 else if(base==16 && *nptr>='a' && *nptr<='f')
1248 ret += *nptr-'a'+10;
1249 else if(base>=10 && *nptr>='0' && *nptr<='9')
1251 else if(base>=8 && *nptr>='0' && *nptr<='7')
1260 static ssize_t es8323_store(struct device *dev,
1261 struct device_attribute *attr,
1262 const char *_buf, size_t _count)
1264 const char * p=_buf;
1267 if(!strncmp(_buf, "get", strlen("get")))
1270 cur_reg=(u32)strtol(p, 16);
1271 val=snd_soc_read(es8323_codec, cur_reg);
1272 printk("%s(): get 0x%04x=0x%04x\n", __FUNCTION__, cur_reg, val);
1274 else if(!strncmp(_buf, "put", strlen("put")))
1278 p=strchr(_buf, '=');
1283 snd_soc_write(es8323_codec, reg, val);
1284 printk("%s(): set 0x%04x=0x%04x\n", __FUNCTION__, reg, val);
1287 printk("%s(): Bad string format input!\n", __FUNCTION__);
1290 printk("%s(): Bad string format input!\n", __FUNCTION__);
1295 static struct device *es8323_dev = NULL;
1296 static struct class *es8323_class = NULL;
1297 static DEVICE_ATTR(es8323, 0664, es8323_show, es8323_store);
1302 compatible = "es8323";
1304 spk-con-gpio = <&gpio2 GPIO_D7 GPIO_ACTIVE_HIGH>;
1305 hp-det-gpio = <&gpio0 GPIO_B5 GPIO_ACTIVE_HIGH>;
1309 static int es8323_i2c_probe(struct i2c_client *i2c,
1310 const struct i2c_device_id *id)
1313 struct es8323_priv *es8323;
1315 struct i2c_adapter *adapter = to_i2c_adapter(i2c->dev.parent);
1318 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
1319 dev_warn(&adapter->dev,
1320 "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
1324 es8323 = kzalloc(sizeof(struct es8323_priv), GFP_KERNEL);
1328 i2c_set_clientdata(i2c, es8323);
1329 es8323->control_type = SND_SOC_I2C;
1331 es8323_spk_con_gpio = of_get_named_gpio(i2c->dev.of_node, "spk-con-gpio", 0);
1332 if (es8323_spk_con_gpio < 0) {
1333 DBG("%s() Can not read property codec-en-gpio\n", __FUNCTION__);
1334 es8323_spk_con_gpio = INVALID_GPIO;
1337 es8323_hp_det_gpio = of_get_named_gpio(i2c->dev.of_node, "hp-det-gpio", 0);
1338 if (es8323_hp_det_gpio < 0) {
1339 DBG("%s() Can not read property codec-en-gpio\n", __FUNCTION__);
1340 es8323_hp_det_gpio = INVALID_GPIO;
1343 reg = ES8323_DACCONTROL18;
1344 ret = i2c_master_recv(i2c, ®, 1);
1345 //ret =i2c_master_reg8_recv(client, 0x00, buf, 2, 200*1000);//i2c_write_bytes(client, &test_data, 1); //Test I2C connection.
1347 printk("es8323 probe error\n");
1352 printk("es8323 probe i2c recv ok\n");
1354 ret = snd_soc_register_codec(&i2c->dev,
1355 &soc_codec_dev_es8323, &es8323_dai, 1);
1360 es8323_class = class_create(THIS_MODULE, "es8323");
1361 if (IS_ERR(es8323_class))
1363 printk("Create class audio_es8323.\n");
1366 es8323_dev = device_create(es8323_class, NULL, MKDEV(0, 1), NULL, "dev");
1367 ret = device_create_file(es8323_dev, &dev_attr_es8323);
1369 printk("failed to add dev_attr_es8323 file\n");
1370 #ifdef CONFIG_MACH_RK_FAC
1377 static int es8323_i2c_remove(struct i2c_client *client)
1379 snd_soc_unregister_codec(&client->dev);
1380 kfree(i2c_get_clientdata(client));
1384 static const struct i2c_device_id es8323_i2c_id[] = {
1388 MODULE_DEVICE_TABLE(i2c, es8323_i2c_id);
1390 void es8323_i2c_shutdown(struct i2c_client *client)
1392 printk("Chenzy-------hkw-------%s\n", __func__);
1393 gpio_direction_output(es8323_spk_con_gpio,0);
1395 snd_soc_write(es8323_codec, ES8323_CONTROL2, 0x58);
1396 snd_soc_write(es8323_codec, ES8323_CONTROL1, 0x32);
1397 snd_soc_write(es8323_codec, ES8323_CHIPPOWER, 0xf3);
1398 snd_soc_write(es8323_codec, ES8323_DACPOWER, 0xc0);
1400 snd_soc_write(es8323_codec, ES8323_DACCONTROL26, 0x00);
1401 snd_soc_write(es8323_codec, ES8323_DACCONTROL27, 0x00);
1403 snd_soc_write(es8323_codec, ES8323_CONTROL1, 0x30);
1404 snd_soc_write(es8323_codec, ES8323_CONTROL1, 0x34);
1409 static int es8323_i2c_suspend (struct i2c_client *client)
1414 static int es8323_i2c_resume(struct i2c_client *client)
1419 static struct i2c_driver es8323_i2c_driver = {
1422 .owner = THIS_MODULE,
1424 .probe = es8323_i2c_probe,
1425 .remove = es8323_i2c_remove,
1426 .shutdown = es8323_i2c_shutdown,
1427 .suspend = es8323_i2c_suspend,
1428 .resume = es8323_i2c_resume,
1429 .id_table = es8323_i2c_id,
1433 static int __init es8323_modinit(void)
1435 return i2c_add_driver(&es8323_i2c_driver);
1437 module_init(es8323_modinit);
1439 static void __exit es8323_exit(void)
1441 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1442 i2c_del_driver(&es8323_i2c_driver);
1444 #if defined(CONFIG_SPI_MASTER)
1445 spi_unregister_driver(&es8323_spi_driver);
1448 module_exit(es8323_exit);
1451 MODULE_DESCRIPTION("ASoC es8323 driver");
1452 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1453 MODULE_LICENSE("GPL");