xref: /linux/sound/soc/codecs/ssm2602.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * File:         sound/soc/codecs/ssm2602.c
3  * Author:       Cliff Cai <Cliff.Cai@analog.com>
4  *
5  * Created:      Tue June 06 2008
6  * Description:  Driver for ssm2602 sound chip
7  *
8  * Modified:
9  *               Copyright 2008 Analog Devices Inc.
10  *
11  * Bugs:         Enter bugs at http://blackfin.uclinux.org/
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, see the file COPYING, or write
25  * to the Free Software Foundation, Inc.,
26  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
27  */
28 
29 #include <linux/module.h>
30 #include <linux/regmap.h>
31 #include <linux/slab.h>
32 
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/tlv.h>
37 
38 #include "ssm2602.h"
39 
40 /* codec private data */
41 struct ssm2602_priv {
42 	unsigned int sysclk;
43 	const struct snd_pcm_hw_constraint_list *sysclk_constraints;
44 
45 	struct regmap *regmap;
46 
47 	enum ssm2602_type type;
48 	unsigned int clk_out_pwr;
49 };
50 
51 /*
52  * ssm2602 register cache
53  * We can't read the ssm2602 register space when we are
54  * using 2 wire for device control, so we cache them instead.
55  * There is no point in caching the reset register
56  */
57 static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
58 	0x0097, 0x0097, 0x0079, 0x0079,
59 	0x000a, 0x0008, 0x009f, 0x000a,
60 	0x0000, 0x0000
61 };
62 
63 
64 /*Appending several "None"s just for OSS mixer use*/
65 static const char *ssm2602_input_select[] = {
66 	"Line", "Mic",
67 };
68 
69 static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
70 
71 static const struct soc_enum ssm2602_enum[] = {
72 	SOC_ENUM_SINGLE(SSM2602_APANA, 2, ARRAY_SIZE(ssm2602_input_select),
73 			ssm2602_input_select),
74 	SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, ARRAY_SIZE(ssm2602_deemph),
75 			ssm2602_deemph),
76 };
77 
78 static const DECLARE_TLV_DB_RANGE(ssm260x_outmix_tlv,
79 	0, 47, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
80 	48, 127, TLV_DB_SCALE_ITEM(-7400, 100, 0)
81 );
82 
83 static const DECLARE_TLV_DB_SCALE(ssm260x_inpga_tlv, -3450, 150, 0);
84 static const DECLARE_TLV_DB_SCALE(ssm260x_sidetone_tlv, -1500, 300, 0);
85 
86 static const struct snd_kcontrol_new ssm260x_snd_controls[] = {
87 SOC_DOUBLE_R_TLV("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 45, 0,
88 	ssm260x_inpga_tlv),
89 SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
90 
91 SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
92 SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
93 
94 SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
95 };
96 
97 static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
98 SOC_DOUBLE_R_TLV("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
99 	0, 127, 0, ssm260x_outmix_tlv),
100 SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
101 	7, 1, 0),
102 SOC_SINGLE_TLV("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1,
103 	ssm260x_sidetone_tlv),
104 
105 SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
106 SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
107 SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
108 };
109 
110 /* Output Mixer */
111 static const struct snd_kcontrol_new ssm260x_output_mixer_controls[] = {
112 SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
113 SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
114 SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
115 };
116 
117 /* Input mux */
118 static const struct snd_kcontrol_new ssm2602_input_mux_controls =
119 SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
120 
121 static const struct snd_soc_dapm_widget ssm260x_dapm_widgets[] = {
122 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
123 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
124 SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
125 
126 SND_SOC_DAPM_SUPPLY("Digital Core Power", SSM2602_ACTIVE, 0, 0, NULL, 0),
127 
128 SND_SOC_DAPM_OUTPUT("LOUT"),
129 SND_SOC_DAPM_OUTPUT("ROUT"),
130 SND_SOC_DAPM_INPUT("RLINEIN"),
131 SND_SOC_DAPM_INPUT("LLINEIN"),
132 };
133 
134 static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
135 SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
136 	ssm260x_output_mixer_controls,
137 	ARRAY_SIZE(ssm260x_output_mixer_controls)),
138 
139 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
140 SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
141 
142 SND_SOC_DAPM_OUTPUT("LHPOUT"),
143 SND_SOC_DAPM_OUTPUT("RHPOUT"),
144 SND_SOC_DAPM_INPUT("MICIN"),
145 };
146 
147 static const struct snd_soc_dapm_widget ssm2604_dapm_widgets[] = {
148 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
149 	ssm260x_output_mixer_controls,
150 	ARRAY_SIZE(ssm260x_output_mixer_controls) - 1), /* Last element is the mic */
151 };
152 
153 static const struct snd_soc_dapm_route ssm260x_routes[] = {
154 	{"DAC", NULL, "Digital Core Power"},
155 	{"ADC", NULL, "Digital Core Power"},
156 
157 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
158 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
159 
160 	{"ROUT", NULL, "Output Mixer"},
161 	{"LOUT", NULL, "Output Mixer"},
162 
163 	{"Line Input", NULL, "LLINEIN"},
164 	{"Line Input", NULL, "RLINEIN"},
165 };
166 
167 static const struct snd_soc_dapm_route ssm2602_routes[] = {
168 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
169 
170 	{"RHPOUT", NULL, "Output Mixer"},
171 	{"LHPOUT", NULL, "Output Mixer"},
172 
173 	{"Input Mux", "Line", "Line Input"},
174 	{"Input Mux", "Mic", "Mic Bias"},
175 	{"ADC", NULL, "Input Mux"},
176 
177 	{"Mic Bias", NULL, "MICIN"},
178 };
179 
180 static const struct snd_soc_dapm_route ssm2604_routes[] = {
181 	{"ADC", NULL, "Line Input"},
182 };
183 
184 static const unsigned int ssm2602_rates_12288000[] = {
185 	8000, 16000, 32000, 48000, 96000,
186 };
187 
188 static const struct snd_pcm_hw_constraint_list ssm2602_constraints_12288000 = {
189 	.list = ssm2602_rates_12288000,
190 	.count = ARRAY_SIZE(ssm2602_rates_12288000),
191 };
192 
193 static const unsigned int ssm2602_rates_11289600[] = {
194 	8000, 11025, 22050, 44100, 88200,
195 };
196 
197 static const struct snd_pcm_hw_constraint_list ssm2602_constraints_11289600 = {
198 	.list = ssm2602_rates_11289600,
199 	.count = ARRAY_SIZE(ssm2602_rates_11289600),
200 };
201 
202 struct ssm2602_coeff {
203 	u32 mclk;
204 	u32 rate;
205 	u8 srate;
206 };
207 
208 #define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
209 
210 /* codec mclk clock coefficients */
211 static const struct ssm2602_coeff ssm2602_coeff_table[] = {
212 	/* 48k */
213 	{12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
214 	{18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
215 	{12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
216 
217 	/* 32k */
218 	{12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
219 	{18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
220 	{12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
221 
222 	/* 16k */
223 	{12288000, 16000, SSM2602_COEFF_SRATE(0x5, 0x0, 0x0)},
224 	{18432000, 16000, SSM2602_COEFF_SRATE(0x5, 0x1, 0x0)},
225 	{12000000, 16000, SSM2602_COEFF_SRATE(0xa, 0x0, 0x1)},
226 
227 	/* 8k */
228 	{12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
229 	{18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
230 	{11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
231 	{16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
232 	{12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
233 
234 	/* 96k */
235 	{12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
236 	{18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
237 	{12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
238 
239 	/* 11.025k */
240 	{11289600, 11025, SSM2602_COEFF_SRATE(0xc, 0x0, 0x0)},
241 	{16934400, 11025, SSM2602_COEFF_SRATE(0xc, 0x1, 0x0)},
242 	{12000000, 11025, SSM2602_COEFF_SRATE(0xc, 0x1, 0x1)},
243 
244 	/* 22.05k */
245 	{11289600, 22050, SSM2602_COEFF_SRATE(0xd, 0x0, 0x0)},
246 	{16934400, 22050, SSM2602_COEFF_SRATE(0xd, 0x1, 0x0)},
247 	{12000000, 22050, SSM2602_COEFF_SRATE(0xd, 0x1, 0x1)},
248 
249 	/* 44.1k */
250 	{11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
251 	{16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
252 	{12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
253 
254 	/* 88.2k */
255 	{11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
256 	{16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
257 	{12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
258 };
259 
260 static inline int ssm2602_get_coeff(int mclk, int rate)
261 {
262 	int i;
263 
264 	for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
265 		if (ssm2602_coeff_table[i].rate == rate &&
266 			ssm2602_coeff_table[i].mclk == mclk)
267 			return ssm2602_coeff_table[i].srate;
268 	}
269 	return -EINVAL;
270 }
271 
272 static int ssm2602_hw_params(struct snd_pcm_substream *substream,
273 	struct snd_pcm_hw_params *params,
274 	struct snd_soc_dai *dai)
275 {
276 	struct snd_soc_codec *codec = dai->codec;
277 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
278 	int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
279 	unsigned int iface;
280 
281 	if (srate < 0)
282 		return srate;
283 
284 	regmap_write(ssm2602->regmap, SSM2602_SRATE, srate);
285 
286 	/* bit size */
287 	switch (params_width(params)) {
288 	case 16:
289 		iface = 0x0;
290 		break;
291 	case 20:
292 		iface = 0x4;
293 		break;
294 	case 24:
295 		iface = 0x8;
296 		break;
297 	case 32:
298 		iface = 0xc;
299 		break;
300 	default:
301 		return -EINVAL;
302 	}
303 	regmap_update_bits(ssm2602->regmap, SSM2602_IFACE,
304 		IFACE_AUDIO_DATA_LEN, iface);
305 	return 0;
306 }
307 
308 static int ssm2602_startup(struct snd_pcm_substream *substream,
309 			   struct snd_soc_dai *dai)
310 {
311 	struct snd_soc_codec *codec = dai->codec;
312 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
313 
314 	if (ssm2602->sysclk_constraints) {
315 		snd_pcm_hw_constraint_list(substream->runtime, 0,
316 				   SNDRV_PCM_HW_PARAM_RATE,
317 				   ssm2602->sysclk_constraints);
318 	}
319 
320 	return 0;
321 }
322 
323 static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
324 {
325 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(dai->codec);
326 
327 	if (mute)
328 		regmap_update_bits(ssm2602->regmap, SSM2602_APDIGI,
329 				    APDIGI_ENABLE_DAC_MUTE,
330 				    APDIGI_ENABLE_DAC_MUTE);
331 	else
332 		regmap_update_bits(ssm2602->regmap, SSM2602_APDIGI,
333 				    APDIGI_ENABLE_DAC_MUTE, 0);
334 	return 0;
335 }
336 
337 static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
338 		int clk_id, unsigned int freq, int dir)
339 {
340 	struct snd_soc_codec *codec = codec_dai->codec;
341 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
342 
343 	if (dir == SND_SOC_CLOCK_IN) {
344 		if (clk_id != SSM2602_SYSCLK)
345 			return -EINVAL;
346 
347 		switch (freq) {
348 		case 12288000:
349 		case 18432000:
350 			ssm2602->sysclk_constraints = &ssm2602_constraints_12288000;
351 			break;
352 		case 11289600:
353 		case 16934400:
354 			ssm2602->sysclk_constraints = &ssm2602_constraints_11289600;
355 			break;
356 		case 12000000:
357 			ssm2602->sysclk_constraints = NULL;
358 			break;
359 		default:
360 			return -EINVAL;
361 		}
362 		ssm2602->sysclk = freq;
363 	} else {
364 		unsigned int mask;
365 
366 		switch (clk_id) {
367 		case SSM2602_CLK_CLKOUT:
368 			mask = PWR_CLK_OUT_PDN;
369 			break;
370 		case SSM2602_CLK_XTO:
371 			mask = PWR_OSC_PDN;
372 			break;
373 		default:
374 			return -EINVAL;
375 		}
376 
377 		if (freq == 0)
378 			ssm2602->clk_out_pwr |= mask;
379 		else
380 			ssm2602->clk_out_pwr &= ~mask;
381 
382 		regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
383 			PWR_CLK_OUT_PDN | PWR_OSC_PDN, ssm2602->clk_out_pwr);
384 	}
385 
386 	return 0;
387 }
388 
389 static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
390 		unsigned int fmt)
391 {
392 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec_dai->codec);
393 	unsigned int iface = 0;
394 
395 	/* set master/slave audio interface */
396 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
397 	case SND_SOC_DAIFMT_CBM_CFM:
398 		iface |= 0x0040;
399 		break;
400 	case SND_SOC_DAIFMT_CBS_CFS:
401 		break;
402 	default:
403 		return -EINVAL;
404 	}
405 
406 	/* interface format */
407 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
408 	case SND_SOC_DAIFMT_I2S:
409 		iface |= 0x0002;
410 		break;
411 	case SND_SOC_DAIFMT_RIGHT_J:
412 		break;
413 	case SND_SOC_DAIFMT_LEFT_J:
414 		iface |= 0x0001;
415 		break;
416 	case SND_SOC_DAIFMT_DSP_A:
417 		iface |= 0x0013;
418 		break;
419 	case SND_SOC_DAIFMT_DSP_B:
420 		iface |= 0x0003;
421 		break;
422 	default:
423 		return -EINVAL;
424 	}
425 
426 	/* clock inversion */
427 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
428 	case SND_SOC_DAIFMT_NB_NF:
429 		break;
430 	case SND_SOC_DAIFMT_IB_IF:
431 		iface |= 0x0090;
432 		break;
433 	case SND_SOC_DAIFMT_IB_NF:
434 		iface |= 0x0080;
435 		break;
436 	case SND_SOC_DAIFMT_NB_IF:
437 		iface |= 0x0010;
438 		break;
439 	default:
440 		return -EINVAL;
441 	}
442 
443 	/* set iface */
444 	regmap_write(ssm2602->regmap, SSM2602_IFACE, iface);
445 	return 0;
446 }
447 
448 static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
449 				 enum snd_soc_bias_level level)
450 {
451 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
452 
453 	switch (level) {
454 	case SND_SOC_BIAS_ON:
455 		/* vref/mid on, osc and clkout on if enabled */
456 		regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
457 			PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
458 			ssm2602->clk_out_pwr);
459 		break;
460 	case SND_SOC_BIAS_PREPARE:
461 		break;
462 	case SND_SOC_BIAS_STANDBY:
463 		/* everything off except vref/vmid, */
464 		regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
465 			PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
466 			PWR_CLK_OUT_PDN | PWR_OSC_PDN);
467 		break;
468 	case SND_SOC_BIAS_OFF:
469 		/* everything off */
470 		regmap_update_bits(ssm2602->regmap, SSM2602_PWR,
471 			PWR_POWER_OFF, PWR_POWER_OFF);
472 		break;
473 
474 	}
475 	return 0;
476 }
477 
478 #define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
479 		SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
480 		SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
481 		SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
482 		SNDRV_PCM_RATE_96000)
483 
484 #define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
485 		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
486 
487 static const struct snd_soc_dai_ops ssm2602_dai_ops = {
488 	.startup	= ssm2602_startup,
489 	.hw_params	= ssm2602_hw_params,
490 	.digital_mute	= ssm2602_mute,
491 	.set_sysclk	= ssm2602_set_dai_sysclk,
492 	.set_fmt	= ssm2602_set_dai_fmt,
493 };
494 
495 static struct snd_soc_dai_driver ssm2602_dai = {
496 	.name = "ssm2602-hifi",
497 	.playback = {
498 		.stream_name = "Playback",
499 		.channels_min = 2,
500 		.channels_max = 2,
501 		.rates = SSM2602_RATES,
502 		.formats = SSM2602_FORMATS,},
503 	.capture = {
504 		.stream_name = "Capture",
505 		.channels_min = 2,
506 		.channels_max = 2,
507 		.rates = SSM2602_RATES,
508 		.formats = SSM2602_FORMATS,},
509 	.ops = &ssm2602_dai_ops,
510 	.symmetric_rates = 1,
511 	.symmetric_samplebits = 1,
512 };
513 
514 static int ssm2602_resume(struct snd_soc_codec *codec)
515 {
516 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
517 
518 	regcache_sync(ssm2602->regmap);
519 
520 	return 0;
521 }
522 
523 static int ssm2602_codec_probe(struct snd_soc_codec *codec)
524 {
525 	struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
526 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
527 	int ret;
528 
529 	regmap_update_bits(ssm2602->regmap, SSM2602_LOUT1V,
530 			    LOUT1V_LRHP_BOTH, LOUT1V_LRHP_BOTH);
531 	regmap_update_bits(ssm2602->regmap, SSM2602_ROUT1V,
532 			    ROUT1V_RLHP_BOTH, ROUT1V_RLHP_BOTH);
533 
534 	ret = snd_soc_add_codec_controls(codec, ssm2602_snd_controls,
535 			ARRAY_SIZE(ssm2602_snd_controls));
536 	if (ret)
537 		return ret;
538 
539 	ret = snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
540 			ARRAY_SIZE(ssm2602_dapm_widgets));
541 	if (ret)
542 		return ret;
543 
544 	return snd_soc_dapm_add_routes(dapm, ssm2602_routes,
545 			ARRAY_SIZE(ssm2602_routes));
546 }
547 
548 static int ssm2604_codec_probe(struct snd_soc_codec *codec)
549 {
550 	struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
551 	int ret;
552 
553 	ret = snd_soc_dapm_new_controls(dapm, ssm2604_dapm_widgets,
554 			ARRAY_SIZE(ssm2604_dapm_widgets));
555 	if (ret)
556 		return ret;
557 
558 	return snd_soc_dapm_add_routes(dapm, ssm2604_routes,
559 			ARRAY_SIZE(ssm2604_routes));
560 }
561 
562 static int ssm260x_codec_probe(struct snd_soc_codec *codec)
563 {
564 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
565 	int ret;
566 
567 	ret = regmap_write(ssm2602->regmap, SSM2602_RESET, 0);
568 	if (ret < 0) {
569 		dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
570 		return ret;
571 	}
572 
573 	/* set the update bits */
574 	regmap_update_bits(ssm2602->regmap, SSM2602_LINVOL,
575 			    LINVOL_LRIN_BOTH, LINVOL_LRIN_BOTH);
576 	regmap_update_bits(ssm2602->regmap, SSM2602_RINVOL,
577 			    RINVOL_RLIN_BOTH, RINVOL_RLIN_BOTH);
578 	/*select Line in as default input*/
579 	regmap_write(ssm2602->regmap, SSM2602_APANA, APANA_SELECT_DAC |
580 			APANA_ENABLE_MIC_BOOST);
581 
582 	switch (ssm2602->type) {
583 	case SSM2602:
584 		ret = ssm2602_codec_probe(codec);
585 		break;
586 	case SSM2604:
587 		ret = ssm2604_codec_probe(codec);
588 		break;
589 	}
590 
591 	return ret;
592 }
593 
594 static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
595 	.probe =	ssm260x_codec_probe,
596 	.resume =	ssm2602_resume,
597 	.set_bias_level = ssm2602_set_bias_level,
598 	.suspend_bias_off = true,
599 
600 	.controls = ssm260x_snd_controls,
601 	.num_controls = ARRAY_SIZE(ssm260x_snd_controls),
602 	.dapm_widgets = ssm260x_dapm_widgets,
603 	.num_dapm_widgets = ARRAY_SIZE(ssm260x_dapm_widgets),
604 	.dapm_routes = ssm260x_routes,
605 	.num_dapm_routes = ARRAY_SIZE(ssm260x_routes),
606 };
607 
608 static bool ssm2602_register_volatile(struct device *dev, unsigned int reg)
609 {
610 	return reg == SSM2602_RESET;
611 }
612 
613 const struct regmap_config ssm2602_regmap_config = {
614 	.val_bits = 9,
615 	.reg_bits = 7,
616 
617 	.max_register = SSM2602_RESET,
618 	.volatile_reg = ssm2602_register_volatile,
619 
620 	.cache_type = REGCACHE_RBTREE,
621 	.reg_defaults_raw = ssm2602_reg,
622 	.num_reg_defaults_raw = ARRAY_SIZE(ssm2602_reg),
623 };
624 EXPORT_SYMBOL_GPL(ssm2602_regmap_config);
625 
626 int ssm2602_probe(struct device *dev, enum ssm2602_type type,
627 	struct regmap *regmap)
628 {
629 	struct ssm2602_priv *ssm2602;
630 
631 	if (IS_ERR(regmap))
632 		return PTR_ERR(regmap);
633 
634 	ssm2602 = devm_kzalloc(dev, sizeof(*ssm2602), GFP_KERNEL);
635 	if (ssm2602 == NULL)
636 		return -ENOMEM;
637 
638 	dev_set_drvdata(dev, ssm2602);
639 	ssm2602->type = type;
640 	ssm2602->regmap = regmap;
641 
642 	return snd_soc_register_codec(dev, &soc_codec_dev_ssm2602,
643 		&ssm2602_dai, 1);
644 }
645 EXPORT_SYMBOL_GPL(ssm2602_probe);
646 
647 MODULE_DESCRIPTION("ASoC SSM2602/SSM2603/SSM2604 driver");
648 MODULE_AUTHOR("Cliff Cai");
649 MODULE_LICENSE("GPL");
650