blob: 2361b02bb067b0abdb85d55625ef830f1537b0ec [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * es8312.c -- ES8312/ES8312 ALSA SoC Audio Codec
3 *
4 * Copyright (C) 2018 Everest Semiconductor Co., Ltd
5 *
6 * Authors: David Yang(yangxiaohua@everest-semi.com)
7 *
8 * Based on es8374.c by David Yang(yangxiaohua@everest-semi.com)
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/delay.h>
18#include <linux/pm.h>
19#include <linux/i2c.h>
20#include <linux/slab.h>
21#include <linux/regmap.h>
22#include <linux/stddef.h>
23#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/tlv.h>
27#include <sound/soc.h>
28#include <sound/initval.h>
29
30#include <mach/gpio.h>
31#include <mach/pcu.h>
32#include <linux/gpio.h>
33#include <sound/jack.h>
34#include <linux/irq.h>
35
36#include "es8312.h"
37//#include <linux/soc/zte/pm/drv_idle.h>
38#include "linux/soc/zte/pm/drv_idle.h"
39#include "linux/wakelock.h"
40
41/*
42 * es8312 register cache
43 */
44static struct reg_default es8312_reg_defaults[] = {
45 { 0x00, 0x1f },
46 { 0x01, 0x00 },
47 { 0x02, 0x00 },
48 { 0x03, 0x10 },
49 { 0x04, 0x10 },
50 { 0x05, 0x00 },
51 { 0x06, 0x03 },
52 { 0x07, 0x00 },
53 { 0x08, 0xff },
54 { 0x09, 0x00 },
55 { 0x0a, 0x00 },
56 { 0x0b, 0x00 },
57 { 0x0c, 0x20 },
58 { 0x0d, 0xfc },
59 { 0x0e, 0x6a },
60 { 0x0f, 0x00 },
61
62 { 0x10, 0x13 },
63 { 0x11, 0x7c },
64 { 0x12, 0x02 },
65 { 0x13, 0x40 },
66 { 0x14, 0x10 },
67 { 0x15, 0x00 },
68 { 0x16, 0x04 },
69 { 0x17, 0x00 },
70 { 0x18, 0x00 },
71 { 0x19, 0x00 },
72 { 0x1a, 0x00 },
73 { 0x1b, 0x0c },
74 { 0x1c, 0x4c },
75 { 0x1d, 0x00 },
76 { 0x1e, 0x00 },
77 { 0x1f, 0x00 },
78
79 { 0x20, 0x00 },
80 { 0x21, 0x00 },
81 { 0x22, 0x00 },
82 { 0x23, 0x00 },
83 { 0x24, 0x00 },
84 { 0x25, 0x00 },
85 { 0x26, 0x00 },
86 { 0x27, 0x00 },
87 { 0x28, 0x00 },
88 { 0x29, 0x00 },
89 { 0x2a, 0x00 },
90 { 0x2b, 0x00 },
91 { 0x2c, 0x00 },
92 { 0x2d, 0x00 },
93 { 0x2e, 0x00 },
94 { 0x2f, 0x00 },
95
96 { 0x30, 0x00 },
97 { 0x31, 0x00 },
98 { 0x32, 0x00 },
99 { 0x33, 0x00 },
100 { 0x34, 0x00 },
101 { 0x35, 0x00 },
102 { 0x36, 0x00 },
103 { 0x37, 0x08 },
104 { 0x38, 0x00 },
105 { 0x39, 0x00 },
106 { 0x3a, 0x00 },
107 { 0x3b, 0x00 },
108 { 0x3c, 0x00 },
109 { 0x3d, 0x00 },
110 { 0x3e, 0x00 },
111 { 0x3f, 0x00 },
112
113 { 0x40, 0x00 },
114 { 0x41, 0x00 },
115 { 0x42, 0x00 },
116 { 0x43, 0x00 },
117 { 0x44, 0x00 },
118 { 0x45, 0x00 },
119
120};
121struct sp_config {
122 u8 spc, mmcc, spfs;
123 u32 srate;
124 u8 lrcdiv;
125 u8 sclkdiv;
126};
127
128/* codec private data */
129struct es8312_private {
130 struct snd_soc_codec *codec;
131 struct regmap *regmap;
132 u32 mclk;
133 bool sclkinv;
134 bool mclkinv;
135 bool dmic_enable;
136 struct mutex codec_mutex;
137 struct wake_lock pm_lock;
138 volatile int codec_active_count;
139};
140
141struct es8312_private *es8312_data;
142struct snd_soc_codec *es8312_codec;
143
144static bool es8312_volatile_register(struct device *dev,
145 unsigned int reg)
146{
147 if ((reg <= 0xff)) {
148 return true;
149 }
150 else {
151 return false;
152 }
153}
154
155static bool es8312_readable_register(struct device *dev,
156 unsigned int reg)
157{
158 if ((reg <= 0xff)) {
159 return true;
160 }
161 else {
162 return false;
163 }
164}
165static bool es8312_writable_register(struct device *dev,
166 unsigned int reg)
167{
168 if ((reg <= 0xff)) {
169 return true;
170 }
171 else {
172 return false;
173 }
174}
175
176static const DECLARE_TLV_DB_SCALE(vdac_tlv,
177 -9550, 50, true);
178static const DECLARE_TLV_DB_SCALE(vadc_tlv,
179 -9550, 50, true);
180static const DECLARE_TLV_DB_SCALE(mic_pga_tlv,
181 0, 300, true);
182static const DECLARE_TLV_DB_SCALE(fm_pga_tlv,
183 0, 600, true);
184static const DECLARE_TLV_DB_SCALE(mono_pga_tlv,
185 0, 600, true);
186static const DECLARE_TLV_DB_SCALE(adc_scale_tlv,
187 0, 600, false);
188static const DECLARE_TLV_DB_SCALE(alc_winsize_tlv,
189 0, 25, false);
190static const DECLARE_TLV_DB_SCALE(alc_maxlevel_tlv,
191 -3600, 200, false);
192static const DECLARE_TLV_DB_SCALE(alc_minlevel_tlv,
193 -3600, 200, false);
194static const DECLARE_TLV_DB_SCALE(alc_noisegate_tlv,
195 -9600, 600, false);
196static const DECLARE_TLV_DB_SCALE(alc_noisegate_winsize_tlv,
197 4200, 4200, false);
198static const DECLARE_TLV_DB_SCALE(alc_automute_gain_tlv,
199 4200, 4200, false);
200static const DECLARE_TLV_DB_SCALE(adc_ramprate_tlv,
201 0, 25, false);
202
203static const char * const dmic_type_txt[] = {
204 "dmic at high level",
205 "dmic at low level"
206};
207static const struct soc_enum dmic_type =
208SOC_ENUM_SINGLE(ES8312_ADC_REG15, 0, 1, dmic_type_txt);
209
210static const char * const automute_type_txt[] = {
211 "automute disabled",
212 "automute enable"
213};
214static const struct soc_enum alc_automute_type =
215SOC_ENUM_SINGLE(ES8312_ADC_REG18, 6, 1, automute_type_txt);
216
217static const char * const dacdsm_mute_type_txt[] = {
218 "mute to 8",
219 "mute to 7/9"
220};
221static const struct soc_enum dacdsm_mute_type =
222SOC_ENUM_SINGLE(ES8312_DAC_REG31, 7, 1, dacdsm_mute_type_txt);
223
224static const char * const aec_type_txt[] = {
225 "adc left, adc right",
226 "adc left, null right",
227 "null left, adc right",
228 "null left, null right",
229 "dac left, adc right",
230 "adc left, dac right",
231 "dac left, dac right",
232 "N/A"
233};
234static const struct soc_enum aec_type =
235SOC_ENUM_SINGLE(ES8312_GPIO_REG44, 4, 7, aec_type_txt);
236
237static const char * const adc2dac_sel_txt[] = {
238 "disable",
239 "adc data to dac",
240};
241static const struct soc_enum adc2dac_sel =
242SOC_ENUM_SINGLE(ES8312_GPIO_REG44, 7, 1, adc2dac_sel_txt);
243
244static const char * const mclk_sel_txt[] = {
245 "from mclk pin",
246 "from bclk",
247};
248static const struct soc_enum mclk_src =
249SOC_ENUM_SINGLE(ES8312_CLK_MANAGER_REG01, 7, 1, mclk_sel_txt);
250
251/*
252 * es8312 Controls
253 */
254static const struct snd_kcontrol_new es8312_snd_controls[] = {
255 SOC_SINGLE_TLV("MIC PGA GAIN", ES8312_SYSTEM_REG14,
256 0, 10, 0, mic_pga_tlv),
257 SOC_SINGLE_TLV("FM GAIN", ES8312_SYSTEM_REG13,
258 6, 2, 0, fm_pga_tlv),
259 SOC_SINGLE("FM Switch", ES8312_SYSTEM_REG13, 5, 1, 0),
260 SOC_SINGLE_TLV("ADC SCALE", ES8312_ADC_REG16,
261 0, 7, 0, adc_scale_tlv),
262 SOC_ENUM("DMIC TYPE", dmic_type),
263 SOC_SINGLE_TLV("ADC RAMP RATE", ES8312_ADC_REG15,
264 4, 15, 0, adc_ramprate_tlv),
265 SOC_SINGLE("ADC SDP MUTE", ES8312_SDPOUT_REG0A, 6, 1, 0),
266 SOC_SINGLE("ADC INVERTED", ES8312_ADC_REG16, 4, 1, 0),
267 SOC_SINGLE("ADC SYNC", ES8312_ADC_REG16, 5, 1, 1),
268 SOC_SINGLE("ADC RAM CLR", ES8312_ADC_REG16, 3, 1, 0),
269 SOC_SINGLE_TLV("ADC VOLUME", ES8312_ADC_REG17,
270 0, 255, 0, vadc_tlv),
271 SOC_SINGLE("ALC ENABLE", ES8312_ADC_REG18, 7, 1, 0),
272 SOC_ENUM("ALC AUTOMUTE TYPE", alc_automute_type),
273 SOC_SINGLE_TLV("ALC WIN SIZE", ES8312_ADC_REG18,
274 0, 15, 0, alc_winsize_tlv),
275 SOC_SINGLE_TLV("ALC MAX LEVEL", ES8312_ADC_REG19,
276 4, 15, 0, alc_maxlevel_tlv),
277 SOC_SINGLE_TLV("ALC MIN LEVEL", ES8312_ADC_REG19,
278 0, 15, 0, alc_minlevel_tlv),
279 SOC_SINGLE_TLV("ALC AUTOMUTE WINSIZE", ES8312_ADC_REG1A,
280 4, 15, 0, alc_noisegate_winsize_tlv),
281 SOC_SINGLE_TLV("ALC AUTOMUTE GATE THRESHOLD", ES8312_ADC_REG1A,
282 0, 15, 0, alc_noisegate_tlv),
283 SOC_SINGLE_TLV("ALC AUTOMUTE VOLUME", ES8312_ADC_REG1B,
284 5, 7, 0, alc_automute_gain_tlv),
285 SOC_SINGLE("ADC FS MODE", ES8312_CLK_MANAGER_REG03, 6, 1, 0),
286 SOC_SINGLE("ADC OSR", ES8312_CLK_MANAGER_REG03, 0, 63, 0),
287 SOC_SINGLE("DAC SDP MUTE", ES8312_SDPIN_REG09, 6, 1, 0),
288 SOC_SINGLE("DAC DEM MUTE", ES8312_DAC_REG31, 5, 1, 0),
289 SOC_SINGLE("DAC INVERT", ES8312_DAC_REG31, 4, 1, 0),
290 SOC_SINGLE("DAC RAM CLR", ES8312_DAC_REG31, 3, 1, 0),
291 SOC_ENUM("DAC DSM MUTE", dacdsm_mute_type),
292 SOC_SINGLE("DAC OFFSET", ES8312_DAC_REG33, 0, 255, 0),
293 SOC_SINGLE_TLV("DAC VOLUME", ES8312_DAC_REG32,
294 0, 255, 0, vdac_tlv),
295 SOC_SINGLE("DRC ENABLE", ES8312_DAC_REG34, 7, 1, 0),
296 SOC_SINGLE_TLV("DRC WIN SIZE", ES8312_DAC_REG34,
297 0, 15, 0, alc_winsize_tlv),
298 SOC_SINGLE_TLV("DRC MAX LEVEL", ES8312_DAC_REG35,
299 4, 15, 0, alc_maxlevel_tlv),
300 SOC_SINGLE_TLV("DRC MIN LEVEL", ES8312_DAC_REG35,
301 0, 15, 0, alc_minlevel_tlv),
302 SOC_SINGLE_TLV("DAC RAMP RATE", ES8312_DAC_REG37,
303 4, 15, 0, adc_ramprate_tlv),
304 SOC_SINGLE("DAC OSR", ES8312_CLK_MANAGER_REG04, 0, 127, 0),
305 SOC_ENUM("AEC MODE", aec_type),
306 SOC_ENUM("ADC DATA TO DAC TEST MODE", adc2dac_sel),
307 SOC_SINGLE("MCLK INVERT", ES8312_CLK_MANAGER_REG01, 6, 1, 0),
308 SOC_SINGLE("BCLK INVERT", ES8312_CLK_MANAGER_REG06, 5, 1, 0),
309// SOC_ENUM("MCLK SOURCE", mclk_src),
310};
311
312/*
313* adc line in selection
314*/
315static const char * const es8312_adc_input_src_txt[] = {
316 "NO-IN",
317 "MIC1P-MIC1N",
318 "MIC2P-MIC2N",
319 "MIC1-MIC2",
320};
321static const unsigned int es8312_adc_input_src_values[] = {
322 0, 1, 2, 3};
323static const struct soc_enum es8312_adc_input_src_enum =
324SOC_VALUE_ENUM_SINGLE(ES8312_SYSTEM_REG14, 4, 3,
325 ARRAY_SIZE(es8312_adc_input_src_txt),
326 es8312_adc_input_src_txt,
327 es8312_adc_input_src_values);
328static const struct snd_kcontrol_new es8312_adc_input_src_controls =
329 SOC_DAPM_ENUM("Route", es8312_adc_input_src_enum);
330
331/*
332 * DAPM Controls
333 */
334static const char * const es8312_dmic_mux_txt[] = {
335 "DMIC DISABLE",
336 "DMIC ENABLE"
337};
338static const unsigned int es8312_dmic_mux_values[] = {
339 0, 1
340};
341static const struct soc_enum es8312_dmic_mux_enum =
342 SOC_VALUE_ENUM_SINGLE(ES8312_SYSTEM_REG14, 6, 1,
343 ARRAY_SIZE(es8312_dmic_mux_txt),
344 es8312_dmic_mux_txt,
345 es8312_dmic_mux_values);
346static const struct snd_kcontrol_new es8312_dmic_mux_controls =
347 SOC_DAPM_ENUM("DMIC ROUTE", es8312_dmic_mux_enum);
348
349static const char * const es8312_adc_sdp_mux_txt[] = {
350 "FROM EQUALIZER",
351 "FROM ADC OUT",
352};
353static const unsigned int es8312_adc_sdp_mux_values[] = {
354 0, 1
355};
356static const struct soc_enum es8312_adc_sdp_mux_enum =
357 SOC_VALUE_ENUM_SINGLE(ES8312_ADC_REG1C, 6, 1,
358 ARRAY_SIZE(es8312_adc_sdp_mux_txt),
359 es8312_adc_sdp_mux_txt,
360 es8312_adc_sdp_mux_values);
361static const struct snd_kcontrol_new es8312_adc_sdp_mux_controls =
362 SOC_DAPM_ENUM("ADC SDP ROUTE", es8312_adc_sdp_mux_enum);
363
364/*
365* alc on/off
366*/
367static const char * const es8312_alc_enable_txt[] = {
368 "ALC OFF",
369 "ALC ON",
370};
371static const unsigned int es8312_alc_enable_values[] = {
372 0, 1};
373static const struct soc_enum es8312_alc_enable_enum =
374SOC_VALUE_ENUM_SINGLE(ES8312_ADC_REG18, 7, 1,
375 ARRAY_SIZE(es8312_alc_enable_txt),
376 es8312_alc_enable_txt,
377 es8312_alc_enable_values);
378static const struct snd_kcontrol_new es8312_alc_enable_controls =
379 SOC_DAPM_ENUM("Route", es8312_alc_enable_enum);
380
381/*
382* DAC data soure
383*/
384static const char * const es8312_dac_data_mux_txt[] = {
385 "SELECT SDP LEFT DATA",
386 "SELECT SDP RIGHT DATA",
387};
388static const unsigned int es8312_dac_data_mux_values[] = {
389 0, 1
390};
391static const struct soc_enum es8312_dac_data_mux_enum =
392 SOC_VALUE_ENUM_SINGLE(ES8312_SDPIN_REG09, 7, 1,
393 ARRAY_SIZE(es8312_dac_data_mux_txt),
394 es8312_dac_data_mux_txt,
395 es8312_dac_data_mux_values);
396static const struct snd_kcontrol_new es8312_dac_data_mux_controls =
397 SOC_DAPM_ENUM("DAC SDP ROUTE", es8312_dac_data_mux_enum);
398
399
400/*
401* DRC
402*/
403static const char * const es8312_drc_mux_txt[] = {
404 "DRC DISABLE",
405 "DRC ENABLE",
406};
407static const unsigned int es8312_drc_mux_values[] = {
408 0, 1
409};
410static const struct soc_enum es8312_drc_mux_enum =
411 SOC_VALUE_ENUM_SINGLE(ES8312_DAC_REG34, 7, 1,
412 ARRAY_SIZE(es8312_drc_mux_txt),
413 es8312_drc_mux_txt,
414 es8312_drc_mux_values);
415static const struct snd_kcontrol_new es8312_drc_mux_controls =
416 SOC_DAPM_ENUM("DRC MUX ROUTE", es8312_drc_mux_enum);
417
418/*
419* Dac eq
420*/
421static const char * const es8312_dac_eq_mux_txt[] = {
422 "DAC EQ ENABLE",
423 "DAC EQ BYPASS",
424};
425static const unsigned int es8312_dac_eq_mux_values[] = {
426 0,1
427};
428static const struct soc_enum es8312_dac_eq_mux_enum =
429 SOC_VALUE_ENUM_SINGLE(ES8312_DAC_REG37, 3, 1,
430 ARRAY_SIZE(es8312_dac_eq_mux_txt),
431 es8312_dac_eq_mux_txt,
432 es8312_dac_eq_mux_values);
433static const struct snd_kcontrol_new es8312_dac_eq_mux_controls =
434 SOC_DAPM_ENUM("DAC EQ MUX ROUTE", es8312_dac_eq_mux_enum);
435
436
437
438static const struct snd_soc_dapm_widget es8312_dapm_widgets[] = {
439 /* Input*/
440 SND_SOC_DAPM_INPUT("DMIC"),
441 SND_SOC_DAPM_INPUT("AMIC"),
442
443 /* Input MUX */
444 SND_SOC_DAPM_MUX("DIFFERENTIAL MUX", SND_SOC_NOPM, 0, 0,
445 &es8312_adc_input_src_controls),
446
447 SND_SOC_DAPM_PGA("INPUT PGA", SND_SOC_NOPM,
448 0, 0, NULL, 0),
449 /* ADCs */
450 SND_SOC_DAPM_ADC("MONO ADC", NULL, SND_SOC_NOPM, 0, 0),
451 /* Dmic MUX */
452 SND_SOC_DAPM_MUX("DMIC MUX", SND_SOC_NOPM, 0, 0,
453 &es8312_dmic_mux_controls),
454 /* Dmic MUX */
455 SND_SOC_DAPM_MUX("ALC MUX", SND_SOC_NOPM, 0, 0,
456 &es8312_alc_enable_controls),
457
458 /* sdp MUX */
459 SND_SOC_DAPM_MUX("SDP OUT MUX", SND_SOC_NOPM, 0, 0,
460 &es8312_adc_sdp_mux_controls),
461 /* Digital Interface */
462 SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 1,
463 SND_SOC_NOPM, 0, 0),
464 /* Render path */
465 SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0,
466 SND_SOC_NOPM, 0, 0),
467 /*DACs SDP DATA SRC MUX */
468 SND_SOC_DAPM_MUX("DAC SDP SRC MUX", SND_SOC_NOPM, 0, 0,
469 &es8312_dac_data_mux_controls),
470
471 SND_SOC_DAPM_MUX("DRC MUX", SND_SOC_NOPM, 0, 0,
472 &es8312_drc_mux_controls),
473 SND_SOC_DAPM_MUX("DAC EQ MUX", SND_SOC_NOPM, 0, 0,
474 &es8312_dac_eq_mux_controls),
475
476 SND_SOC_DAPM_DAC("MONO DAC", NULL, SND_SOC_NOPM, 0, 0),
477
478 /* Output Lines */
479 SND_SOC_DAPM_OUTPUT("DIFFERENTIAL OUT"),
480
481};
482
483
484static const struct snd_soc_dapm_route es8312_dapm_routes[] = {
485 /* record route map */
486
487 {"DIFFERENTIAL MUX", "MIC1P-MIC1N", "AMIC"},
488 {"DIFFERENTIAL MUX", "MIC2P-MIC2N", "AMIC"},
489 {"DIFFERENTIAL MUX", "MIC1-MIC2", "AMIC"},
490
491 {"INPUT PGA", NULL, "DIFFERENTIAL MUX"},
492 {"MONO ADC", NULL, "INPUT PGA"},
493 {"DMIC MUX", "DMIC DISABLE", "MONO ADC"},
494 {"DMIC MUX", "DMIC ENABLE", "DMIC"},
495
496 /* Alc Mux */
497 {"ALC MUX", "ALC OFF", "DMIC MUX"},
498 {"ALC MUX", "ALC ON", "DMIC MUX"},
499
500 /*
501 * Equalizer path
502 */
503 {"SDP OUT MUX", "FROM EQUALIZER", "ALC MUX"},
504 {"SDP OUT MUX", "FROM ADC OUT", "ALC MUX"},
505
506 {"I2S OUT", NULL, "SDP OUT MUX"},
507
508 /* playback route map */
509 {"DAC SDP SRC MUX", "SELECT SDP LEFT DATA", "I2S IN"},
510 {"DAC SDP SRC MUX", "SELECT SDP RIGHT DATA", "I2S IN"},
511
512 {"DRC MUX", "DRC DISABLE", "DAC SDP SRC MUX"},
513 {"DRC MUX", "DRC ENABLE", "DAC SDP SRC MUX"},
514
515 {"DAC EQ MUX", "DAC EQ BYPASS", "DRC MUX"},
516 {"DAC EQ MUX", "DAC EQ ENABLE", "DRC MUX"},
517
518 {"MONO DAC", NULL, "DAC EQ MUX"},
519 {"DIFFERENTIAL OUT", NULL, "MONO DAC"},
520};
521
522struct _coeff_div {
523 u32 mclk; /* mclk frequency */
524 u32 rate; /* sample rate */
525 u8 prediv; /* the pre divider with range from 1 to 8 */
526 u8 premulti; /* the pre multiplier with x1, x2, x4 and x8 selection */
527 u8 adcdiv; /* adcclk divider */
528 u8 dacdiv; /* dacclk divider */
529 u8 fsmode; /* double speed or single speed, =0, ss, =1, ds */
530 u8 lrck_h; /* adclrck divider and daclrck divider */
531 u8 lrck_l;
532 u8 bclkdiv; /* sclk divider */
533 u8 adcosr; /* adc osr */
534 u8 dacosr; /* dac osr */
535};
536
537
538/* codec hifi mclk clock divider coefficients */
539static const struct _coeff_div coeff_div[] = {
540 //mclk rate prediv mult adcdiv dacdiv fsmode lrch lrcl bckdiv osr
541 /* 8k */
542 //{26000000, 8000 , 0x05, 0x08, 0x0d, 0x0d, 0x00, 0x01, 0xff, 0x04, 0x19, 0x19},
543 {26000000, 8000 , 0x06, 0x01, 0x02, 0x02, 0x00, 0x01, 0xff, 0x04, 0x10, 0x10},
544 //{26000000, 8000 , 0x05, 0x08, 0x0d, 0x0d, 0x00, 0x01, 0x8f, 0x04, 0x14, 0x14},
545 {24576000, 8000 , 0x06, 0x01, 0x02, 0x02, 0x00, 0x01, 0xff, 0x04, 0x10, 0x10},
546 {12288000, 8000 , 0x06, 0x01, 0x01, 0x01, 0x00, 0x01, 0xff, 0x04, 0x10, 0x10},
547 {18432000, 8000 , 0x03, 0x02, 0x03, 0x03, 0x00, 0x01, 0xff, 0x18, 0x10, 0x10},
548 {16384000, 8000 , 0x08, 0x01, 0x01, 0x01, 0x00, 0x01, 0xff, 0x04, 0x10, 0x10},
549 {8192000 , 8000 , 0x04, 0x01, 0x01, 0x01, 0x00, 0x01, 0xff, 0x04, 0x10, 0x10},
550 {6144000 , 8000 , 0x03, 0x01, 0x01, 0x01, 0x00, 0x01, 0xff, 0x04, 0x10, 0x10},
551 {4096000 , 8000 , 0x02, 0x01, 0x01, 0x01, 0x00, 0x01, 0xff, 0x04, 0x10, 0x10},
552 {3072000 , 8000 , 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0xff, 0x04, 0x10, 0x10},
553 {2048000 , 8000 , 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, 0xff, 0x04, 0x10, 0x10},
554 {1536000 , 8000 , 0x03, 0x04, 0x01, 0x01, 0x00, 0x01, 0xff, 0x04, 0x10, 0x10},
555 {1024000 , 8000 , 0x01, 0x02, 0x01, 0x01, 0x00, 0x01, 0xff, 0x04, 0x10, 0x10},
556
557 /* 11.025k */
558 {26000000, 11025, 0x04, 0x01, 0x02, 0x02, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
559 {24576000, 11025, 0x04, 0x01, 0x02, 0x02, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
560 {11289600, 11025, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
561 {5644800 , 11025, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
562 {2822400 , 11025, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
563 {1411200 , 11025, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
564
565 /* 12k */
566 {26000000, 12000, 0x08, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x19, 0x19},
567 {24576000, 12000, 0x08, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
568 {12288000, 12000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
569 {6144000 , 12000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
570 {3072000 , 12000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
571 {1536000 , 12000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
572
573 /* 16k */
574 {26000000, 16000, 0x06, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
575 {24576000, 16000, 0x06, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
576 {12288000, 16000, 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
577 {18432000, 16000, 0x03, 0x02, 0x03, 0x03, 0x00, 0x02, 0xff, 0x0c, 0x10, 0x10},
578 {16384000, 16000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
579 {8192000 , 16000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
580 {6144000 , 16000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
581 {4096000 , 16000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
582 {3072000 , 16000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
583 {2048000 , 16000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
584 {1536000 , 16000, 0x03, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
585 {1024000 , 16000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
586
587 /* 22.05k */
588 {26000000, 22050, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
589 {24576000, 22050, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
590 {11289600, 22050, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
591 {5644800 , 22050, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
592 {2822400 , 22050, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
593 {1411200 , 22050, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
594
595 /* 24k */
596 {26000000, 24000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
597 {24576000, 24000, 0x04, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
598 {12288000, 24000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
599 {18432000, 24000, 0x03, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
600 {6144000 , 24000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
601 {3072000 , 24000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
602 {1536000 , 24000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
603
604 /* 32k */
605 {26000000, 32000, 0x06, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
606 {24576000, 32000, 0x06, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
607 {12288000, 32000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
608 {18432000, 32000, 0x03, 0x04, 0x03, 0x03, 0x00, 0x02, 0xff, 0x0c, 0x10, 0x10},
609 {16384000, 32000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
610 {8192000 , 32000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
611 {6144000 , 32000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
612 {4096000 , 32000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
613 {3072000 , 32000, 0x03, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
614 {2048000 , 32000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
615 {1536000 , 32000, 0x03, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
616 {1024000 , 32000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
617
618 /* 44.1k */
619 {26000000, 44100, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
620 {24576000, 44100, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
621 {11289600, 44100, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
622 {5644800 , 44100, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
623 {2822400 , 44100, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
624 {1411200 , 44100, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
625
626 /* 48k */
627 {26000000, 48000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
628 {24576000, 48000, 0x02, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
629 {12288000, 48000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
630 {18432000, 48000, 0x03, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
631 {6144000 , 48000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
632 {3072000 , 48000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
633 {1536000 , 48000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
634
635 /* 64k */
636 {26000000, 64000, 0x06, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
637 {24576000, 64000, 0x06, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
638 {12288000, 64000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
639 {18432000, 64000, 0x03, 0x04, 0x03, 0x03, 0x01, 0x01, 0x7f, 0x06, 0x10, 0x10},
640 {16384000, 64000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
641 {8192000 , 64000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
642 {6144000 , 64000, 0x01, 0x04, 0x03, 0x03, 0x01, 0x01, 0x7f, 0x06, 0x10, 0x10},
643 {4096000 , 64000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
644 {3072000 , 64000, 0x01, 0x08, 0x03, 0x03, 0x01, 0x01, 0x7f, 0x06, 0x10, 0x10},
645 {2048000 , 64000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
646 {1536000 , 64000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0xbf, 0x03, 0x18, 0x18},
647 {1024000 , 64000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
648
649 /* 88.2k */
650 {26000000, 88200, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
651 {24576000, 88200, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
652 {11289600, 88200, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
653 {5644800 , 88200, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
654 {2822400 , 88200, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
655 {1411200 , 88200, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
656
657 /* 96k */
658 {26000000, 96000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
659 {24576000, 96000, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
660 {12288000, 96000, 0x01, 0x02, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
661 {18432000, 96000, 0x03, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
662 {6144000 , 96000, 0x01, 0x04, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
663 {3072000 , 96000, 0x01, 0x08, 0x01, 0x01, 0x00, 0x00, 0xff, 0x04, 0x10, 0x10},
664 {1536000 , 96000, 0x01, 0x08, 0x01, 0x01, 0x01, 0x00, 0x7f, 0x02, 0x10, 0x10},
665};
666static inline int get_coeff(int mclk, int rate)
667{
668 int i;
669
670 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
671 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
672 return i;
673 }
674
675 return -EINVAL;
676}
677
678/*
679 * if PLL not be used, use internal clk1 for mclk,otherwise, use internal clk2 for PLL source.
680 */
681static int es8312_set_dai_sysclk(struct snd_soc_dai *dai,
682 int clk_id, unsigned int freq, int dir)
683{
684 struct snd_soc_codec *codec = dai->codec;
685 struct es8312_private *es8312 = snd_soc_codec_get_drvdata(codec);
686 printk("Enter into %s() freq =%d\n", __func__, freq);
687
688 if (clk_id == ES8312_CLKID_BCLK)
689 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG01, 0x80, 0x80);
690 else if (clk_id == ES8312_CLKID_MCLK)
691 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG01, 0x80, 0x00);
692
693 es8312->mclk = freq;
694#if 0
695 switch (freq) {
696 case 11289600:
697 case 22579200:
698 es8312->mclk = freq;
699 return 0;
700
701 case 12288000:
702 case 16384000:
703 case 18432000:
704 case 24576000:
705 case 26000000:
706 es8312->mclk = freq;
707 return 0;
708 }
709#endif
710 return 0;
711}
712
713static int es8312_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
714{
715 struct snd_soc_codec *codec = codec_dai->codec;
716 u8 iface = 0;
717 u8 adciface = 0;
718 u8 daciface = 0;
719 printk("Enter into %s()\n", __func__);
720 iface = snd_soc_read(codec, ES8312_RESET_REG00);
721 adciface = snd_soc_read(codec, ES8312_SDPOUT_REG0A);
722 daciface = snd_soc_read(codec, ES8312_SDPIN_REG09);
723
724 /* set master/slave audio interface */
725 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
726 case SND_SOC_DAIFMT_CBM_CFM: /* MASTER MODE */
727 printk("ES8312 in Master mode\n");
728 iface |= 0x40;
729 break;
730 case SND_SOC_DAIFMT_CBS_CFS: /* SLAVE MODE */
731 printk("ES8312 in Slave mode\n");
732 iface &= 0xBF;
733 break;
734 default:
735 return -EINVAL;
736 }
737 snd_soc_write(codec, ES8312_RESET_REG00, iface);
738
739
740 /* interface format */
741 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
742 case SND_SOC_DAIFMT_I2S:
743 printk("ES8312 in I2S Format\n");
744 adciface &= 0xFC;
745 daciface &= 0xFC;
746 break;
747 case SND_SOC_DAIFMT_RIGHT_J:
748 return -EINVAL;
749 case SND_SOC_DAIFMT_LEFT_J:
750 printk("ES8312 in LJ Format\n");
751 adciface &= 0xFC;
752 daciface &= 0xFC;
753 adciface |= 0x01;
754 daciface |= 0x01;
755 break;
756 case SND_SOC_DAIFMT_DSP_A:
757 printk("ES8312 in DSP-A Format\n");
758 adciface &= 0xDC;
759 daciface &= 0xDC;
760 adciface |= 0x03;
761 daciface |= 0x03;
762 break;
763 case SND_SOC_DAIFMT_DSP_B:
764 printk("ES8312 in DSP-B Format\n");
765 adciface &= 0xDC;
766 daciface &= 0xDC;
767 adciface |= 0x23;
768 daciface |= 0x23;
769 break;
770 default:
771 return -EINVAL;
772 }
773
774 iface = snd_soc_read(codec, ES8312_CLK_MANAGER_REG06);
775 /* clock inversion */
776 if(((fmt & SND_SOC_DAIFMT_FORMAT_MASK)==SND_SOC_DAIFMT_I2S) ||
777 ((fmt & SND_SOC_DAIFMT_FORMAT_MASK)==SND_SOC_DAIFMT_LEFT_J))
778 {
779
780 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
781 case SND_SOC_DAIFMT_NB_NF:
782
783 iface &= 0xDF;
784 adciface &= 0xDF;
785 daciface &= 0xDF;
786 break;
787 case SND_SOC_DAIFMT_IB_IF:
788 iface |= 0x20;
789 adciface |= 0x20;
790 daciface |= 0x20;
791 break;
792 case SND_SOC_DAIFMT_IB_NF:
793 iface |= 0x20;
794 adciface &= 0xDF;
795 daciface &= 0xDF;
796 break;
797 case SND_SOC_DAIFMT_NB_IF:
798 iface &= 0xDF;
799 adciface |= 0x20;
800 daciface |= 0x20;
801 break;
802 default:
803 return -EINVAL;
804 }
805 }
806
807 snd_soc_write(codec, ES8312_CLK_MANAGER_REG06, iface);
808 snd_soc_write(codec, ES8312_SDPOUT_REG0A, adciface);
809 snd_soc_write(codec, ES8312_SDPIN_REG09, daciface);
810 return 0;
811}
812
813static int es8312_pcm_startup(struct snd_pcm_substream *substream,
814 struct snd_soc_dai *dai)
815{
816 return 0;
817}
818
819static int es8312_pcm_shutdown(struct snd_pcm_substream *substream,
820 struct snd_soc_dai *dai)
821{
822 struct snd_soc_pcm_runtime *rtd = substream->private_data;
823 struct snd_soc_codec *codec = rtd->codec;
824 struct es8312_private *es8312 = snd_soc_codec_get_drvdata(codec);
825 printk("Enter into %s()\n", __func__);
826 snd_soc_write(codec, ES8312_DAC_REG32, 0x00);
827 snd_soc_write(codec, ES8312_ADC_REG17, 0x00);
828 snd_soc_write(codec, ES8312_SYSTEM_REG0E, 0xFF);
829 snd_soc_write(codec, ES8312_SYSTEM_REG12, 0x02);
830 snd_soc_write(codec, ES8312_SYSTEM_REG14, 0x00);
831 snd_soc_write(codec, ES8312_SYSTEM_REG0D, 0xFA);
832 snd_soc_write(codec, ES8312_ADC_REG15, 0x00);
833 snd_soc_write(codec, ES8312_DAC_REG37, 0x08);
834 snd_soc_write(codec, ES8312_RESET_REG00, 0x00);
835 snd_soc_write(codec, ES8312_RESET_REG00, 0x1F);
836 snd_soc_write(codec, ES8312_CLK_MANAGER_REG01, 0x30);
837 snd_soc_write(codec, ES8312_CLK_MANAGER_REG01, 0x00);
838 snd_soc_write(codec, ES8312_GP_REG45, 0x01);
839 snd_soc_write(codec, ES8312_SYSTEM_REG0D, 0xFC);
840 return 0;
841}
842
843static int es8312_pcm_hw_params(struct snd_pcm_substream *substream,
844 struct snd_pcm_hw_params *params,
845 struct snd_soc_dai *dai)
846{
847 struct snd_soc_pcm_runtime *rtd = substream->private_data;
848 struct snd_soc_codec *codec = rtd->codec;
849 struct es8312_private *es8312 = snd_soc_codec_get_drvdata(codec);
850 u16 iface;
851 int coeff;
852 u8 regv, datmp;
853 printk("Enter into %s()\n", __func__);
854
855 coeff = get_coeff(es8312->mclk, params_rate(params));
856 if (coeff < 0) {
857 printk("Unable to configure sample rate %dHz with %dHz MCLK\n",
858 params_rate(params), es8312->mclk);
859 return coeff;
860 }
861
862 /*
863 * set clock parammeters
864 */
865 if(coeff >= 0) {
866 snd_soc_write(codec, ES8312_CLK_MANAGER_REG01, 0x30);
867 snd_soc_write(codec, ES8312_RESET_REG00, 0x83);
868 regv = snd_soc_read(codec, ES8312_CLK_MANAGER_REG02) & 0x07;
869 regv |= (coeff_div[coeff].prediv - 1) << 5;
870 datmp = 0;
871 switch(coeff_div[coeff].premulti)
872 {
873 case 1:
874 datmp = 0;
875 break;
876 case 2:
877 datmp = 1;
878 break;
879 case 4:
880 datmp = 2;
881 break;
882 case 8:
883 datmp = 3;
884 break;
885 defalut:
886 break;
887 }
888 regv |= (datmp) << 3;
889 snd_soc_write(codec, ES8312_CLK_MANAGER_REG02, regv);
890
891 regv = snd_soc_read(codec, ES8312_CLK_MANAGER_REG05) & 0x00;
892 regv |= (coeff_div[coeff].adcdiv-1) << 4;
893 regv |= (coeff_div[coeff].dacdiv-1) << 0;
894 snd_soc_write(codec, ES8312_CLK_MANAGER_REG05, regv);
895
896 regv = snd_soc_read(codec, ES8312_CLK_MANAGER_REG03) & 0x80;
897 regv |= coeff_div[coeff].fsmode << 6;
898 regv |= coeff_div[coeff].adcosr << 0;
899 snd_soc_write(codec, ES8312_CLK_MANAGER_REG03, regv);
900
901 regv = snd_soc_read(codec, ES8312_CLK_MANAGER_REG04) & 0x80;
902 regv |= coeff_div[coeff].dacosr << 0;
903 snd_soc_write(codec, ES8312_CLK_MANAGER_REG04, regv);
904
905 regv = snd_soc_read(codec, ES8312_CLK_MANAGER_REG07) & 0xf0;
906 regv |= coeff_div[coeff].lrck_h << 0;
907 snd_soc_write(codec, ES8312_CLK_MANAGER_REG07, regv);
908
909 regv = snd_soc_read(codec, ES8312_CLK_MANAGER_REG08) & 0x00;
910 regv |= coeff_div[coeff].lrck_l << 0;
911 snd_soc_write(codec, ES8312_CLK_MANAGER_REG08, regv);
912
913 regv = snd_soc_read(codec, ES8312_CLK_MANAGER_REG06) & 0xE0;
914 regv |= coeff_div[coeff].bclkdiv << 0;
915 snd_soc_write(codec, ES8312_CLK_MANAGER_REG06, regv);
916
917 snd_soc_write(codec, ES8312_RESET_REG00, 0x80);
918 snd_soc_write(codec, ES8312_CLK_MANAGER_REG01, 0x3F);
919 }
920
921 if(substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
922 iface = snd_soc_read(codec, ES8312_SDPIN_REG09) & 0xE3;
923 /* bit size */
924 switch (params_format(params)) {
925 case SNDRV_PCM_FORMAT_S16_LE:
926 iface |= 0x0c;
927 break;
928 case SNDRV_PCM_FORMAT_S20_3LE:
929 iface |= 0x04;
930 break;
931 case SNDRV_PCM_FORMAT_S24_LE:
932 break;
933 case SNDRV_PCM_FORMAT_S32_LE:
934 iface |= 0x10;
935 break;
936 }
937 /* set iface */
938 snd_soc_write(codec, ES8312_SDPIN_REG09, iface);
939 } else {
940 iface = snd_soc_read(codec, ES8312_SDPOUT_REG0A) & 0xE3;
941 /* bit size */
942
943 /* set iface */
944 snd_soc_write(codec, ES8312_SDPOUT_REG0A, iface);
945 }
946 switch (params_format(params)) {
947 case SNDRV_PCM_FORMAT_S16_LE:
948 iface |= 0x0c;
949 break;
950 case SNDRV_PCM_FORMAT_S20_3LE:
951 iface |= 0x04;
952 break;
953 case SNDRV_PCM_FORMAT_S24_LE:
954 break;
955 case SNDRV_PCM_FORMAT_S32_LE:
956 iface |= 0x10;
957 break;
958 }
959 switch(params_rate(params))
960 {
961 case 8000:
962 case 11025:
963 break;
964 case 16000:
965 case 24000:
966 case 48000:
967 case 22050:
968 case 44100:
969 break;
970 default:
971 break;
972 }
973 return 0;
974}
975extern void zx29_i2s_close_clk(void);
976#ifdef CONFIG_PM
977static void es8312_set_cpu_active(struct es8312_private *es8312)
978{
979 mutex_lock(&es8312->codec_mutex);
980 if(es8312->codec_active_count == 0)
981 {
982 zx_cpuidle_set_busy(IDLE_FLAG_CODEC);
983 wake_lock(&es8312->pm_lock);
984 es8312->codec_active_count++;
985 }
986 mutex_unlock(&es8312->codec_mutex);
987}
988
989static void es8312_set_cpu_idle(struct es8312_private *es8312)
990{
991 mutex_lock(&es8312->codec_mutex);
992 if(es8312->codec_active_count != 0)
993 {
994 es8312->codec_active_count--;
995 zx_cpuidle_set_free(IDLE_FLAG_CODEC);
996 wake_unlock(&es8312->pm_lock);
997 }
998 mutex_unlock(&es8312->codec_mutex);
999}
1000
1001#endif
1002static int es8312_set_bias_level(struct snd_soc_codec *codec,
1003 enum snd_soc_bias_level level)
1004{
1005 int regv;
1006 struct es8312_private *es8312 = snd_soc_codec_get_drvdata(codec);
1007 printk("Enter into %s(), level = %d\n", __func__, level);
1008 switch (level) {
1009 case SND_SOC_BIAS_ON:
1010 snd_soc_write(codec, ES8312_GP_REG45, 0x00);
1011 snd_soc_write(codec, ES8312_ADC_REG16, 0x24);
1012 snd_soc_write(codec, ES8312_SYSTEM_REG0B, 0x00);
1013 snd_soc_write(codec, ES8312_SYSTEM_REG0C, 0x00);
1014 snd_soc_write(codec, ES8312_SYSTEM_REG10, 0x1F);
1015 snd_soc_write(codec, ES8312_SYSTEM_REG11, 0x7F);
1016 snd_soc_write(codec, ES8312_RESET_REG00, 0x80);
1017 snd_soc_write(codec, ES8312_SYSTEM_REG0D, 0x01);
1018 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG01, 0x3F, 0x3F);
1019 if(es8312->mclkinv == true) {
1020 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG01,
1021 0x40, 0x40);
1022 }
1023 else {
1024 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG01,
1025 0x40, 0x00);
1026 }
1027 if(es8312->sclkinv == true) {
1028 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG06,
1029 0x20, 0x20);
1030 }
1031 else {
1032 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG06,
1033 0x20, 0x00);
1034 }
1035 regv = snd_soc_read(codec, ES8312_SYSTEM_REG14) & 0xCF;
1036 regv |= 0x1A;
1037 snd_soc_write(codec, ES8312_SYSTEM_REG14, regv);
1038 if(es8312->dmic_enable == true) {
1039 snd_soc_update_bits(codec, ES8312_SYSTEM_REG14,
1040 0x40, 0x40);
1041 }
1042 else {
1043 snd_soc_update_bits(codec, ES8312_SYSTEM_REG14,
1044 0x40, 0x00);
1045 }
1046 snd_soc_write(codec, ES8312_SYSTEM_REG13, 0x10);
1047 snd_soc_write(codec, ES8312_SYSTEM_REG0E, 0x02);
1048 snd_soc_write(codec, ES8312_ADC_REG15, 0x40);
1049 snd_soc_write(codec, ES8312_ADC_REG1B, 0x0A);
1050 snd_soc_write(codec, ES8312_ADC_REG1C, 0x6A);
1051 snd_soc_write(codec, ES8312_DAC_REG37, 0x48);
1052 snd_soc_write(codec, ES8312_GPIO_REG44, 0x08);
1053 snd_soc_write(codec, ES8312_ADC_REG17, 0xBF);
1054 snd_soc_write(codec, ES8312_DAC_REG32, 0xBF);
1055 snd_soc_write(codec, ES8312_SYSTEM_REG12, 0xc0);
1056 #ifdef CONFIG_PM
1057 es8312_set_cpu_active(es8312);
1058 #endif
1059 break;
1060 case SND_SOC_BIAS_PREPARE:
1061 break;
1062 case SND_SOC_BIAS_STANDBY:
1063 snd_soc_write(codec, ES8312_DAC_REG32, 0x00);
1064 snd_soc_write(codec, ES8312_ADC_REG17, 0x00);
1065 snd_soc_write(codec, ES8312_SYSTEM_REG0E, 0xFF);
1066 snd_soc_write(codec, ES8312_SYSTEM_REG12, 0x02);
1067 snd_soc_write(codec, ES8312_SYSTEM_REG14, 0x00);
1068 snd_soc_write(codec, ES8312_SYSTEM_REG0D, 0xFA);
1069 snd_soc_write(codec, ES8312_ADC_REG15, 0x00);
1070 snd_soc_write(codec, ES8312_DAC_REG37, 0x08);
1071 snd_soc_write(codec, ES8312_RESET_REG00, 0x00);
1072 snd_soc_write(codec, ES8312_RESET_REG00, 0x1F);
1073 snd_soc_write(codec, ES8312_CLK_MANAGER_REG01, 0x30);
1074 snd_soc_write(codec, ES8312_CLK_MANAGER_REG01, 0x00);
1075 snd_soc_write(codec, ES8312_GP_REG45, 0x01);
1076 snd_soc_write(codec, ES8312_SYSTEM_REG0D, 0xFC);
1077 zx29_i2s_close_clk();
1078#ifdef CONFIG_PM
1079 es8312_set_cpu_idle(es8312);
1080#endif
1081 break;
1082 case SND_SOC_BIAS_OFF:
1083 break;
1084 }
1085 codec->dapm.bias_level = level;
1086 return 0;
1087}
1088
1089static int es8312_set_tristate(struct snd_soc_dai *dai, int tristate)
1090{
1091 struct snd_soc_codec *codec = dai->codec;
1092 printk("Enter into %s(), tristate = %d\n", __func__, tristate);
1093 if(tristate) {
1094 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG07,
1095 0x30, 0x30);
1096 }
1097 else {
1098 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG07,
1099 0x30, 0x00);
1100 }
1101 return 0;
1102}
1103
1104static DEFINE_RAW_SPINLOCK(codec_pa_lock);
1105static int es8312_mute(struct snd_soc_dai *dai, int mute)
1106{
1107 struct snd_soc_codec *codec = dai->codec;
1108 unsigned long flags;
1109
1110 printk("Enter into %s(), mute = %d\n", __func__, mute);
1111 if (mute) {
1112 // snd_soc_write(codec, ES8312_SYSTEM_REG12, 0x02);
1113 mdelay(4);
1114 gpio_set_value(ZX29_GPIO_125, GPIO_LOW);
1115 mdelay(1);
1116 snd_soc_update_bits(codec, ES8312_DAC_REG31, 0x68, 0x68);
1117 // snd_soc_write(codec, ES8312_DAC_REG32, 0x00);
1118 // snd_soc_write(codec, ES8312_DAC_REG37, 0x08);
1119 } else if (dai->playback_active) {
1120 gpio_set_value(ZX29_GPIO_125, GPIO_LOW);
1121 mdelay(1);
1122 raw_spin_lock_irqsave(&codec_pa_lock, flags);
1123 gpio_set_value(ZX29_GPIO_125, GPIO_HIGH);
1124 udelay(2);
1125 gpio_set_value(ZX29_GPIO_125, GPIO_LOW);
1126 udelay(2);
1127 gpio_set_value(ZX29_GPIO_125, GPIO_HIGH);
1128 udelay(2);
1129 gpio_set_value(ZX29_GPIO_125, GPIO_LOW);
1130 udelay(2);
1131 gpio_set_value(ZX29_GPIO_125, GPIO_HIGH);
1132 raw_spin_unlock_irqrestore(&codec_pa_lock, flags);
1133 snd_soc_update_bits(codec, ES8312_DAC_REG31, 0x68, 0x00);
1134 // snd_soc_write(codec, ES8312_SYSTEM_REG12, 0x00);
1135 }
1136 return 0;
1137}
1138
1139#define es8312_RATES SNDRV_PCM_RATE_8000_96000
1140
1141#define es8312_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
1142 SNDRV_PCM_FMTBIT_S24_LE)
1143
1144static struct snd_soc_dai_ops es8312_ops = {
1145 .startup = es8312_pcm_startup,
1146 .hw_params = es8312_pcm_hw_params,
1147 .set_fmt = es8312_set_dai_fmt,
1148 .set_sysclk = es8312_set_dai_sysclk,
1149 .digital_mute = es8312_mute,
1150 .set_tristate = es8312_set_tristate,
1151};
1152
1153static struct snd_soc_dai_driver es8312_dai[] = {
1154 {
1155 .name = "ES8312 HiFi",
1156 .playback = {
1157 .stream_name = "Playback",
1158 .channels_min = 1,
1159 .channels_max = 2,
1160 .rates = es8312_RATES,
1161 .formats = es8312_FORMATS,
1162 },
1163 .capture = {
1164 .stream_name = "Capture",
1165 .channels_min = 1,
1166 .channels_max = 2,
1167 .rates = es8312_RATES,
1168 .formats = es8312_FORMATS,
1169 },
1170 .ops = &es8312_ops,
1171 .symmetric_rates = 1,
1172 },
1173};
1174
1175static int es8312_suspend(struct snd_soc_codec *codec)
1176{
1177 printk("Enter into %s()\n", __func__);
1178 snd_soc_write(codec, ES8312_DAC_REG32, 0x00);
1179 snd_soc_write(codec, ES8312_ADC_REG17, 0x00);
1180 snd_soc_write(codec, ES8312_SYSTEM_REG0E, 0xFF);
1181 snd_soc_write(codec, ES8312_SYSTEM_REG12, 0x02);
1182 snd_soc_write(codec, ES8312_SYSTEM_REG14, 0x00);
1183 snd_soc_write(codec, ES8312_SYSTEM_REG0D, 0xFA);
1184 snd_soc_write(codec, ES8312_ADC_REG15, 0x00);
1185 snd_soc_write(codec, ES8312_DAC_REG37, 0x08);
1186 snd_soc_write(codec, ES8312_RESET_REG00, 0x00);
1187 snd_soc_write(codec, ES8312_RESET_REG00, 0x1F);
1188 snd_soc_write(codec, ES8312_CLK_MANAGER_REG01, 0x30);
1189 snd_soc_write(codec, ES8312_CLK_MANAGER_REG01, 0x00);
1190 snd_soc_write(codec, ES8312_GP_REG45, 0x01);
1191 return 0;
1192}
1193
1194static int es8312_resume(struct snd_soc_codec *codec)
1195{
1196 struct es8312_private *es8312 = snd_soc_codec_get_drvdata(codec);
1197 printk("Enter into %s()\n", __func__);
1198 snd_soc_write(codec, ES8312_GP_REG45, 0x00);
1199 snd_soc_write(codec, ES8312_CLK_MANAGER_REG01, 0x30);
1200 snd_soc_write(codec, ES8312_CLK_MANAGER_REG02, 0x00);
1201 snd_soc_write(codec, ES8312_CLK_MANAGER_REG03, 0x10);
1202 snd_soc_write(codec, ES8312_ADC_REG16, 0x24);
1203 snd_soc_write(codec, ES8312_CLK_MANAGER_REG04, 0x10);
1204 snd_soc_write(codec, ES8312_CLK_MANAGER_REG05, 0x00);
1205 snd_soc_write(codec, ES8312_SYSTEM_REG0B, 0x00);
1206 snd_soc_write(codec, ES8312_SYSTEM_REG0C, 0x00);
1207 snd_soc_write(codec, ES8312_SYSTEM_REG10, 0x1F);
1208 snd_soc_write(codec, ES8312_SYSTEM_REG11, 0x7F);
1209 snd_soc_write(codec, ES8312_RESET_REG00, 0x80);
1210 snd_soc_write(codec, ES8312_SYSTEM_REG0D, 0x01);
1211 snd_soc_write(codec, ES8312_CLK_MANAGER_REG01, 0x3F);
1212 if(es8312->mclkinv == true) {
1213 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG01,
1214 0x40, 0x40);
1215 }
1216 else {
1217 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG01,
1218 0x40, 0x00);
1219 }
1220 if(es8312->sclkinv == true) {
1221 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG06,
1222 0x20, 0x20);
1223 }
1224 else {
1225 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG06,
1226 0x20, 0x00);
1227 }
1228 snd_soc_write(codec, ES8312_SYSTEM_REG14, 0x1A);
1229 if(es8312->dmic_enable == true) {
1230 snd_soc_update_bits(codec, ES8312_SYSTEM_REG14,
1231 0x40, 0x40);
1232 }
1233 else {
1234 snd_soc_update_bits(codec, ES8312_SYSTEM_REG14,
1235 0x40, 0x00);
1236 }
1237 snd_soc_write(codec, ES8312_SYSTEM_REG12, 0x00);
1238 snd_soc_write(codec, ES8312_SYSTEM_REG13, 0x10);
1239 snd_soc_write(codec, ES8312_SDPIN_REG09, 0x00);
1240 snd_soc_write(codec, ES8312_SDPOUT_REG0A, 0x00);
1241 snd_soc_write(codec, ES8312_SYSTEM_REG0E, 0x02);
1242 snd_soc_write(codec, ES8312_ADC_REG15, 0x40);
1243 snd_soc_write(codec, ES8312_ADC_REG1B, 0x0A);
1244 snd_soc_write(codec, ES8312_ADC_REG1C, 0x6A);
1245 snd_soc_write(codec, ES8312_DAC_REG37, 0x48);
1246 snd_soc_write(codec, ES8312_GPIO_REG44, 0x08);
1247 snd_soc_write(codec, ES8312_ADC_REG17, 0xBF);
1248 snd_soc_write(codec, ES8312_DAC_REG32, 0xBF);
1249
1250 return 0;
1251}
1252
1253static int es8312_probe(struct snd_soc_codec *codec)
1254{
1255 int ret = 0;
1256 struct es8312_private *es8312 = snd_soc_codec_get_drvdata(codec);
1257 printk("Enter into %s()\n", __func__);
1258 ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_I2C);
1259 if (ret < 0)
1260 return ret;
1261 es8312_codec = codec;
1262 es8312->codec = codec;
1263 codec->cache_bypass = 1;
1264
1265 snd_soc_update_bits(codec, ES8312_RESET_REG00,
1266 0x10, 0x10);
1267 snd_soc_update_bits(codec, ES8312_RESET_REG00,
1268 0x10, 0x00);
1269 snd_soc_write(codec, ES8312_GP_REG45, 0x00);
1270 snd_soc_write(codec, ES8312_CLK_MANAGER_REG01, 0x30);
1271 snd_soc_write(codec, ES8312_CLK_MANAGER_REG02, 0x00);
1272 snd_soc_write(codec, ES8312_CLK_MANAGER_REG03, 0x10);
1273 snd_soc_write(codec, ES8312_ADC_REG16, 0x24);
1274 snd_soc_write(codec, ES8312_CLK_MANAGER_REG04, 0x10);
1275 snd_soc_write(codec, ES8312_CLK_MANAGER_REG05, 0x00);
1276 snd_soc_write(codec, ES8312_SYSTEM_REG0B, 0x00);
1277 snd_soc_write(codec, ES8312_SYSTEM_REG0C, 0x00);
1278 snd_soc_write(codec, ES8312_SYSTEM_REG10, 0x1F);
1279 snd_soc_write(codec, ES8312_SYSTEM_REG11, 0x7F);
1280 snd_soc_write(codec, ES8312_RESET_REG00, 0x80);
1281 snd_soc_write(codec, ES8312_SYSTEM_REG0D, 0x01);
1282 snd_soc_write(codec, ES8312_CLK_MANAGER_REG01, 0x3F);
1283 if(es8312->mclkinv == true) {
1284 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG01,
1285 0x40, 0x40);
1286 }
1287 else {
1288 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG01,
1289 0x40, 0x00);
1290 }
1291 if(es8312->sclkinv == true) {
1292 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG06,
1293 0x20, 0x20);
1294 }
1295 else {
1296 snd_soc_update_bits(codec, ES8312_CLK_MANAGER_REG06,
1297 0x20, 0x00);
1298 }
1299 snd_soc_write(codec, ES8312_SYSTEM_REG14, 0x1A);
1300 if(es8312->dmic_enable == true) {
1301 snd_soc_update_bits(codec, ES8312_SYSTEM_REG14,
1302 0x40, 0x40);
1303 }
1304 else {
1305 snd_soc_update_bits(codec, ES8312_SYSTEM_REG14,
1306 0x40, 0x00);
1307 }
1308 snd_soc_write(codec, ES8312_SYSTEM_REG12, 0x00);
1309 snd_soc_write(codec, ES8312_SYSTEM_REG13, 0x10);
1310 snd_soc_write(codec, ES8312_SDPIN_REG09, 0x00);
1311 snd_soc_write(codec, ES8312_SDPOUT_REG0A, 0x00);
1312 snd_soc_write(codec, ES8312_SYSTEM_REG0E, 0x02);
1313 snd_soc_write(codec, ES8312_ADC_REG15, 0x40);
1314 snd_soc_write(codec, ES8312_ADC_REG1B, 0x0A);
1315 snd_soc_write(codec, ES8312_ADC_REG1C, 0x6A);
1316 snd_soc_write(codec, ES8312_DAC_REG37, 0x48);
1317 snd_soc_write(codec, ES8312_GPIO_REG44, 0x08);
1318 snd_soc_write(codec, ES8312_ADC_REG17, 0xBF);
1319 snd_soc_write(codec, ES8312_DAC_REG32, 0xBF);
1320
1321 msleep(100);
1322 es8312_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1323 return ret;
1324}
1325
1326static int es8312_remove(struct snd_soc_codec *codec)
1327{
1328 es8312_set_bias_level(codec, SND_SOC_BIAS_OFF);
1329 return 0;
1330}
1331
1332static struct snd_soc_codec_driver soc_codec_dev_es8312 = {
1333 .probe = es8312_probe,
1334 .remove = es8312_remove,
1335 .suspend = es8312_suspend,
1336 .resume = es8312_resume,
1337 .set_bias_level = es8312_set_bias_level,
1338
1339 .reg_cache_size = ARRAY_SIZE(es8312_reg_defaults),
1340 .reg_word_size = sizeof(u8),
1341 .reg_cache_default = es8312_reg_defaults,
1342 .controls = es8312_snd_controls,
1343 .num_controls = ARRAY_SIZE(es8312_snd_controls),
1344 .dapm_widgets = es8312_dapm_widgets,
1345 .num_dapm_widgets = ARRAY_SIZE(es8312_dapm_widgets),
1346 .dapm_routes = es8312_dapm_routes,
1347 .num_dapm_routes = ARRAY_SIZE(es8312_dapm_routes),
1348};
1349
1350static struct regmap_config es8312_regmap = {
1351 .reg_bits = 8,
1352 .val_bits = 8,
1353
1354 .max_register = ES8312_MAX_REGISTER,
1355 .reg_defaults = es8312_reg_defaults,
1356 .num_reg_defaults = ARRAY_SIZE(es8312_reg_defaults),
1357 .volatile_reg = es8312_volatile_register,
1358 .writeable_reg = es8312_writable_register,
1359 .readable_reg = es8312_readable_register,
1360 .cache_type = REGCACHE_RBTREE,
1361};
1362
1363#ifdef CONFIG_OF
1364static struct of_device_id es8312_if_dt_ids[] = {
1365 { .compatible = "ambarella,es8312", },
1366 { }
1367};
1368#endif
1369
1370#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1371static void es8312_i2c_shutdown(struct i2c_client *i2c)
1372{
1373 struct snd_soc_codec *codec;
1374 struct es8312_private *es8312;
1375 es8312 = i2c_get_clientdata(i2c);
1376 if(es8312 == NULL){
1377 printk("%s(): NULL pointer es8312\n", __FUNCTION__);
1378 return;
1379 }
1380 codec = es8312->codec;
1381 return;
1382}
1383static u32 cur_reg=0;
1384
1385static ssize_t es8312_show(struct device *dev,
1386 struct device_attribute *attr, char *_buf)
1387{
1388 int ret;
1389 ret = sprintf(_buf, "%s(): get 0x%04x=0x%04x\n",
1390 __FUNCTION__, cur_reg,
1391 snd_soc_read(es8312_codec, cur_reg));
1392 return ret;
1393}
1394
1395static u32 strtol(const char *nptr, int base)
1396{
1397 u32 ret;
1398 if(!nptr || (base!=16 && base!=10 && base!=8))
1399 {
1400 printk("%s(): NULL pointer input\n", __FUNCTION__);
1401 return -1;
1402 }
1403 for(ret=0; *nptr; nptr++)
1404 {
1405 if((base==16 && *nptr>='A' && *nptr<='F') ||
1406 (base==16 && *nptr>='a' && *nptr<='f') ||
1407 (base>=10 && *nptr>='0' && *nptr<='9') ||
1408 (base>=8 && *nptr>='0' && *nptr<='7') )
1409 {
1410 ret *= base;
1411 if(base==16 && *nptr>='A' && *nptr<='F')
1412 ret += *nptr-'A'+10;
1413 else if(base==16 && *nptr>='a' && *nptr<='f')
1414 ret += *nptr-'a'+10;
1415 else if(base>=10 && *nptr>='0' && *nptr<='9')
1416 ret += *nptr-'0';
1417 else if(base>=8 && *nptr>='0' && *nptr<='7')
1418 ret += *nptr-'0';
1419 }
1420 else
1421 return ret;
1422 }
1423 return ret;
1424}
1425
1426static ssize_t es8312_store(struct device *dev,
1427 struct device_attribute *attr,
1428 const char *buf, size_t count)
1429{
1430 int val=0, flag=0;
1431 u8 i=0, reg, num, value_w, value_r;
1432
1433 val = simple_strtol(buf, NULL, 16);
1434 flag = (val >> 16) & 0xFF;
1435
1436 if (flag) {
1437 reg = (val >> 8) & 0xFF;
1438 value_w = val & 0xFF;
1439 printk("\nWrite: start REG:0x%02x,val:0x%02x,count:0x%02x\n",
1440 reg, value_w, flag);
1441 while(flag--) {
1442 snd_soc_write(es8312_codec, reg, value_w);
1443 printk("Write 0x%02x to REG:0x%02x\n",
1444 value_w,
1445 reg);
1446 reg++;
1447 }
1448 }
1449 else {
1450 reg = (val >> 8) & 0xFF;
1451 num = val & 0xff;
1452 printk("\nRead: start REG:0x%02x,count:0x%02x\n",
1453 reg, num);
1454 do {
1455 value_r = 0;
1456 value_r = snd_soc_read(es8312_codec, reg);
1457 printk("REG[0x%02x]: 0x%02x; \n",
1458 reg, value_r);
1459 reg++;
1460 i++;
1461 } while (i<num);
1462 }
1463
1464 return count;
1465}
1466
1467static struct device *es8312_dev = NULL;
1468static struct class *es8312_class = NULL;
1469static DEVICE_ATTR(es8312, 0664, es8312_show, es8312_store);
1470
1471static struct attribute *es8312_debug_attrs[] = {
1472 &dev_attr_es8312.attr,
1473 NULL,
1474};
1475
1476static struct attribute_group es8312_debug_attr_group = {
1477 .name = "es8312_debug",
1478 .attrs = es8312_debug_attrs,
1479};
1480
1481static int es8312_i2c_probe(struct i2c_client *i2c_client,
1482 const struct i2c_device_id *id)
1483{
1484 struct es8312_private *es8312;
1485 int ret = -1;
1486 printk("Enter into %s\n", __func__);
1487 es8312 = devm_kzalloc(&i2c_client->dev,
1488 sizeof(*es8312), GFP_KERNEL);
1489 if (es8312 == NULL)
1490 return -ENOMEM;
1491
1492 printk("Enter into %s---1\n", __func__);
1493 es8312->dmic_enable = false; // dmic interface disabled
1494 /* the edge of lrck is always at the falling edge of mclk */
1495 es8312->mclkinv = false;
1496 /* the edge of lrck is always at the falling edge of sclk */
1497 es8312->sclkinv = false;
1498
1499 printk("Enter into %s----2\n", __func__);
1500 dev_set_drvdata(&i2c_client->dev, es8312);
1501 es8312_data = es8312;
1502 printk("Enter into %s---3\n", __func__);
1503 ret = snd_soc_register_codec(&i2c_client->dev,
1504 &soc_codec_dev_es8312,
1505 &es8312_dai[0],
1506 ARRAY_SIZE(es8312_dai));
1507 if (ret < 0) {
1508 kfree(es8312);
1509 return ret;
1510 }
1511
1512 printk("Enter into %s-----4\n", __func__);
1513 ret = sysfs_create_group(&i2c_client->dev.kobj,
1514 &es8312_debug_attr_group);
1515 if (ret) {
1516 pr_err("failed to create attr group\n");
1517 }
1518 ret = gpio_request_one(ZX29_GPIO_125, GPIOF_OUT_INIT_LOW, "codec_pa");
1519 if (ret < 0) {
1520 printk(KERN_ERR "Alsa es8374: codec_pa in use\n");
1521 return ret;
1522 }
1523 #ifdef CONFIG_PM
1524 es8312->codec_active_count = 0;
1525 mutex_init(&es8312->codec_mutex);
1526 wake_lock_init(&es8312->pm_lock,WAKE_LOCK_SUSPEND,"es8312-code");
1527 #endif
1528 printk("Exit %s\n", __func__);
1529 return ret;
1530}
1531
1532static int es8312_i2c_remove(struct i2c_client *client)
1533{
1534 snd_soc_unregister_codec(&client->dev);
1535 kfree(i2c_get_clientdata(client));
1536 return 0;
1537}
1538
1539static const struct i2c_device_id es8312_i2c_id[] = {
1540 {"es8312", 0 },
1541 { }
1542};
1543MODULE_DEVICE_TABLE(i2c, es8312_i2c_id);
1544
1545static struct i2c_driver es8312_i2c_driver = {
1546 .driver = {
1547 .name = "es8312",
1548 .owner = THIS_MODULE,
1549#ifdef CONFIG_OF
1550 .of_match_table = of_match_ptr(es8312_if_dt_ids),
1551#endif
1552 },
1553 .shutdown = es8312_i2c_shutdown,
1554 .probe = es8312_i2c_probe,
1555 .remove = es8312_i2c_remove,
1556 .id_table = es8312_i2c_id,
1557};
1558#endif
1559static int __init es8312_init(void)
1560{
1561 return i2c_add_driver(&es8312_i2c_driver);
1562}
1563
1564static void __exit es8312_exit(void)
1565{
1566 return i2c_del_driver(&es8312_i2c_driver);
1567}
1568
1569module_init(es8312_init);
1570module_exit(es8312_exit);
1571
1572MODULE_DESCRIPTION("ASoC es8312 driver");
1573MODULE_AUTHOR("David Yang <yangxiaohua@everest-semi.com>");
1574MODULE_LICENSE("GPL");
1575