xref: /openbmc/linux/sound/soc/codecs/ssm2602.c (revision 9ffc93f2)
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/moduleparam.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/pm.h>
34 #include <linux/i2c.h>
35 #include <linux/spi/spi.h>
36 #include <linux/slab.h>
37 #include <sound/core.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/soc.h>
41 #include <sound/initval.h>
42 #include <sound/tlv.h>
43 
44 #include "ssm2602.h"
45 
46 #define SSM2602_VERSION "0.1"
47 
48 enum ssm2602_type {
49 	SSM2602,
50 	SSM2604,
51 };
52 
53 /* codec private data */
54 struct ssm2602_priv {
55 	unsigned int sysclk;
56 	enum snd_soc_control_type control_type;
57 	struct snd_pcm_substream *master_substream;
58 	struct snd_pcm_substream *slave_substream;
59 
60 	enum ssm2602_type type;
61 	unsigned int clk_out_pwr;
62 };
63 
64 /*
65  * ssm2602 register cache
66  * We can't read the ssm2602 register space when we are
67  * using 2 wire for device control, so we cache them instead.
68  * There is no point in caching the reset register
69  */
70 static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
71 	0x0097, 0x0097, 0x0079, 0x0079,
72 	0x000a, 0x0008, 0x009f, 0x000a,
73 	0x0000, 0x0000
74 };
75 
76 #define ssm2602_reset(c)	snd_soc_write(c, SSM2602_RESET, 0)
77 
78 /*Appending several "None"s just for OSS mixer use*/
79 static const char *ssm2602_input_select[] = {
80 	"Line", "Mic", "None", "None", "None",
81 	"None", "None", "None",
82 };
83 
84 static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
85 
86 static const struct soc_enum ssm2602_enum[] = {
87 	SOC_ENUM_SINGLE(SSM2602_APANA, 2, 2, ssm2602_input_select),
88 	SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph),
89 };
90 
91 static const unsigned int ssm260x_outmix_tlv[] = {
92 	TLV_DB_RANGE_HEAD(2),
93 	0, 47, TLV_DB_SCALE_ITEM(TLV_DB_GAIN_MUTE, 0, 0),
94 	48, 127, TLV_DB_SCALE_ITEM(-7400, 100, 0),
95 };
96 
97 static const DECLARE_TLV_DB_SCALE(ssm260x_inpga_tlv, -3450, 150, 0);
98 static const DECLARE_TLV_DB_SCALE(ssm260x_sidetone_tlv, -1500, 300, 0);
99 
100 static const struct snd_kcontrol_new ssm260x_snd_controls[] = {
101 SOC_DOUBLE_R_TLV("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 45, 0,
102 	ssm260x_inpga_tlv),
103 SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
104 
105 SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
106 SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
107 
108 SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
109 };
110 
111 static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
112 SOC_DOUBLE_R_TLV("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
113 	0, 127, 0, ssm260x_outmix_tlv),
114 SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
115 	7, 1, 0),
116 SOC_SINGLE_TLV("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1,
117 	ssm260x_sidetone_tlv),
118 
119 SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
120 SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 8, 1, 0),
121 SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
122 };
123 
124 /* Output Mixer */
125 static const struct snd_kcontrol_new ssm260x_output_mixer_controls[] = {
126 SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
127 SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
128 SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
129 };
130 
131 /* Input mux */
132 static const struct snd_kcontrol_new ssm2602_input_mux_controls =
133 SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
134 
135 static const struct snd_soc_dapm_widget ssm260x_dapm_widgets[] = {
136 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
137 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
138 SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
139 
140 SND_SOC_DAPM_SUPPLY("Digital Core Power", SSM2602_ACTIVE, 0, 0, NULL, 0),
141 
142 SND_SOC_DAPM_OUTPUT("LOUT"),
143 SND_SOC_DAPM_OUTPUT("ROUT"),
144 SND_SOC_DAPM_INPUT("RLINEIN"),
145 SND_SOC_DAPM_INPUT("LLINEIN"),
146 };
147 
148 static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
149 SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
150 	ssm260x_output_mixer_controls,
151 	ARRAY_SIZE(ssm260x_output_mixer_controls)),
152 
153 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
154 SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
155 
156 SND_SOC_DAPM_OUTPUT("LHPOUT"),
157 SND_SOC_DAPM_OUTPUT("RHPOUT"),
158 SND_SOC_DAPM_INPUT("MICIN"),
159 };
160 
161 static const struct snd_soc_dapm_widget ssm2604_dapm_widgets[] = {
162 SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
163 	ssm260x_output_mixer_controls,
164 	ARRAY_SIZE(ssm260x_output_mixer_controls) - 1), /* Last element is the mic */
165 };
166 
167 static const struct snd_soc_dapm_route ssm260x_routes[] = {
168 	{"DAC", NULL, "Digital Core Power"},
169 	{"ADC", NULL, "Digital Core Power"},
170 
171 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
172 	{"Output Mixer", "HiFi Playback Switch", "DAC"},
173 
174 	{"ROUT", NULL, "Output Mixer"},
175 	{"LOUT", NULL, "Output Mixer"},
176 
177 	{"Line Input", NULL, "LLINEIN"},
178 	{"Line Input", NULL, "RLINEIN"},
179 };
180 
181 static const struct snd_soc_dapm_route ssm2602_routes[] = {
182 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
183 
184 	{"RHPOUT", NULL, "Output Mixer"},
185 	{"LHPOUT", NULL, "Output Mixer"},
186 
187 	{"Input Mux", "Line", "Line Input"},
188 	{"Input Mux", "Mic", "Mic Bias"},
189 	{"ADC", NULL, "Input Mux"},
190 
191 	{"Mic Bias", NULL, "MICIN"},
192 };
193 
194 static const struct snd_soc_dapm_route ssm2604_routes[] = {
195 	{"ADC", NULL, "Line Input"},
196 };
197 
198 struct ssm2602_coeff {
199 	u32 mclk;
200 	u32 rate;
201 	u8 srate;
202 };
203 
204 #define SSM2602_COEFF_SRATE(sr, bosr, usb) (((sr) << 2) | ((bosr) << 1) | (usb))
205 
206 /* codec mclk clock coefficients */
207 static const struct ssm2602_coeff ssm2602_coeff_table[] = {
208 	/* 48k */
209 	{12288000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x0)},
210 	{18432000, 48000, SSM2602_COEFF_SRATE(0x0, 0x1, 0x0)},
211 	{12000000, 48000, SSM2602_COEFF_SRATE(0x0, 0x0, 0x1)},
212 
213 	/* 32k */
214 	{12288000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x0)},
215 	{18432000, 32000, SSM2602_COEFF_SRATE(0x6, 0x1, 0x0)},
216 	{12000000, 32000, SSM2602_COEFF_SRATE(0x6, 0x0, 0x1)},
217 
218 	/* 8k */
219 	{12288000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x0)},
220 	{18432000, 8000, SSM2602_COEFF_SRATE(0x3, 0x1, 0x0)},
221 	{11289600, 8000, SSM2602_COEFF_SRATE(0xb, 0x0, 0x0)},
222 	{16934400, 8000, SSM2602_COEFF_SRATE(0xb, 0x1, 0x0)},
223 	{12000000, 8000, SSM2602_COEFF_SRATE(0x3, 0x0, 0x1)},
224 
225 	/* 96k */
226 	{12288000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x0)},
227 	{18432000, 96000, SSM2602_COEFF_SRATE(0x7, 0x1, 0x0)},
228 	{12000000, 96000, SSM2602_COEFF_SRATE(0x7, 0x0, 0x1)},
229 
230 	/* 44.1k */
231 	{11289600, 44100, SSM2602_COEFF_SRATE(0x8, 0x0, 0x0)},
232 	{16934400, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x0)},
233 	{12000000, 44100, SSM2602_COEFF_SRATE(0x8, 0x1, 0x1)},
234 
235 	/* 88.2k */
236 	{11289600, 88200, SSM2602_COEFF_SRATE(0xf, 0x0, 0x0)},
237 	{16934400, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x0)},
238 	{12000000, 88200, SSM2602_COEFF_SRATE(0xf, 0x1, 0x1)},
239 };
240 
241 static inline int ssm2602_get_coeff(int mclk, int rate)
242 {
243 	int i;
244 
245 	for (i = 0; i < ARRAY_SIZE(ssm2602_coeff_table); i++) {
246 		if (ssm2602_coeff_table[i].rate == rate &&
247 			ssm2602_coeff_table[i].mclk == mclk)
248 			return ssm2602_coeff_table[i].srate;
249 	}
250 	return -EINVAL;
251 }
252 
253 static int ssm2602_hw_params(struct snd_pcm_substream *substream,
254 	struct snd_pcm_hw_params *params,
255 	struct snd_soc_dai *dai)
256 {
257 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
258 	struct snd_soc_codec *codec = rtd->codec;
259 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
260 	u16 iface = snd_soc_read(codec, SSM2602_IFACE) & 0xfff3;
261 	int srate = ssm2602_get_coeff(ssm2602->sysclk, params_rate(params));
262 
263 	if (substream == ssm2602->slave_substream) {
264 		dev_dbg(codec->dev, "Ignoring hw_params for slave substream\n");
265 		return 0;
266 	}
267 
268 	if (srate < 0)
269 		return srate;
270 
271 	snd_soc_write(codec, SSM2602_SRATE, srate);
272 
273 	/* bit size */
274 	switch (params_format(params)) {
275 	case SNDRV_PCM_FORMAT_S16_LE:
276 		break;
277 	case SNDRV_PCM_FORMAT_S20_3LE:
278 		iface |= 0x0004;
279 		break;
280 	case SNDRV_PCM_FORMAT_S24_LE:
281 		iface |= 0x0008;
282 		break;
283 	case SNDRV_PCM_FORMAT_S32_LE:
284 		iface |= 0x000c;
285 		break;
286 	}
287 	snd_soc_write(codec, SSM2602_IFACE, iface);
288 	return 0;
289 }
290 
291 static int ssm2602_startup(struct snd_pcm_substream *substream,
292 			   struct snd_soc_dai *dai)
293 {
294 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
295 	struct snd_soc_codec *codec = rtd->codec;
296 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
297 	struct snd_pcm_runtime *master_runtime;
298 
299 	/* The DAI has shared clocks so if we already have a playback or
300 	 * capture going then constrain this substream to match it.
301 	 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
302 	 */
303 	if (ssm2602->master_substream) {
304 		master_runtime = ssm2602->master_substream->runtime;
305 		dev_dbg(codec->dev, "Constraining to %d bits at %dHz\n",
306 			master_runtime->sample_bits,
307 			master_runtime->rate);
308 
309 		if (master_runtime->rate != 0)
310 			snd_pcm_hw_constraint_minmax(substream->runtime,
311 						     SNDRV_PCM_HW_PARAM_RATE,
312 						     master_runtime->rate,
313 						     master_runtime->rate);
314 
315 		if (master_runtime->sample_bits != 0)
316 			snd_pcm_hw_constraint_minmax(substream->runtime,
317 						     SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
318 						     master_runtime->sample_bits,
319 						     master_runtime->sample_bits);
320 
321 		ssm2602->slave_substream = substream;
322 	} else
323 		ssm2602->master_substream = substream;
324 
325 	return 0;
326 }
327 
328 static void ssm2602_shutdown(struct snd_pcm_substream *substream,
329 			     struct snd_soc_dai *dai)
330 {
331 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
332 	struct snd_soc_codec *codec = rtd->codec;
333 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
334 
335 	if (ssm2602->master_substream == substream)
336 		ssm2602->master_substream = ssm2602->slave_substream;
337 
338 	ssm2602->slave_substream = NULL;
339 }
340 
341 
342 static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
343 {
344 	struct snd_soc_codec *codec = dai->codec;
345 
346 	if (mute)
347 		snd_soc_update_bits(codec, SSM2602_APDIGI,
348 				    APDIGI_ENABLE_DAC_MUTE,
349 				    APDIGI_ENABLE_DAC_MUTE);
350 	else
351 		snd_soc_update_bits(codec, SSM2602_APDIGI,
352 				    APDIGI_ENABLE_DAC_MUTE, 0);
353 	return 0;
354 }
355 
356 static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
357 		int clk_id, unsigned int freq, int dir)
358 {
359 	struct snd_soc_codec *codec = codec_dai->codec;
360 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
361 
362 	if (dir == SND_SOC_CLOCK_IN) {
363 		if (clk_id != SSM2602_SYSCLK)
364 			return -EINVAL;
365 
366 		switch (freq) {
367 		case 11289600:
368 		case 12000000:
369 		case 12288000:
370 		case 16934400:
371 		case 18432000:
372 			ssm2602->sysclk = freq;
373 			break;
374 		default:
375 			return -EINVAL;
376 		}
377 	} else {
378 		unsigned int mask;
379 
380 		switch (clk_id) {
381 		case SSM2602_CLK_CLKOUT:
382 			mask = PWR_CLK_OUT_PDN;
383 			break;
384 		case SSM2602_CLK_XTO:
385 			mask = PWR_OSC_PDN;
386 			break;
387 		default:
388 			return -EINVAL;
389 		}
390 
391 		if (freq == 0)
392 			ssm2602->clk_out_pwr |= mask;
393 		else
394 			ssm2602->clk_out_pwr &= ~mask;
395 
396 		snd_soc_update_bits(codec, SSM2602_PWR,
397 			PWR_CLK_OUT_PDN | PWR_OSC_PDN, ssm2602->clk_out_pwr);
398 	}
399 
400 	return 0;
401 }
402 
403 static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
404 		unsigned int fmt)
405 {
406 	struct snd_soc_codec *codec = codec_dai->codec;
407 	u16 iface = 0;
408 
409 	/* set master/slave audio interface */
410 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
411 	case SND_SOC_DAIFMT_CBM_CFM:
412 		iface |= 0x0040;
413 		break;
414 	case SND_SOC_DAIFMT_CBS_CFS:
415 		break;
416 	default:
417 		return -EINVAL;
418 	}
419 
420 	/* interface format */
421 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
422 	case SND_SOC_DAIFMT_I2S:
423 		iface |= 0x0002;
424 		break;
425 	case SND_SOC_DAIFMT_RIGHT_J:
426 		break;
427 	case SND_SOC_DAIFMT_LEFT_J:
428 		iface |= 0x0001;
429 		break;
430 	case SND_SOC_DAIFMT_DSP_A:
431 		iface |= 0x0013;
432 		break;
433 	case SND_SOC_DAIFMT_DSP_B:
434 		iface |= 0x0003;
435 		break;
436 	default:
437 		return -EINVAL;
438 	}
439 
440 	/* clock inversion */
441 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
442 	case SND_SOC_DAIFMT_NB_NF:
443 		break;
444 	case SND_SOC_DAIFMT_IB_IF:
445 		iface |= 0x0090;
446 		break;
447 	case SND_SOC_DAIFMT_IB_NF:
448 		iface |= 0x0080;
449 		break;
450 	case SND_SOC_DAIFMT_NB_IF:
451 		iface |= 0x0010;
452 		break;
453 	default:
454 		return -EINVAL;
455 	}
456 
457 	/* set iface */
458 	snd_soc_write(codec, SSM2602_IFACE, iface);
459 	return 0;
460 }
461 
462 static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
463 				 enum snd_soc_bias_level level)
464 {
465 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
466 
467 	switch (level) {
468 	case SND_SOC_BIAS_ON:
469 		/* vref/mid on, osc and clkout on if enabled */
470 		snd_soc_update_bits(codec, SSM2602_PWR,
471 			PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
472 			ssm2602->clk_out_pwr);
473 		break;
474 	case SND_SOC_BIAS_PREPARE:
475 		break;
476 	case SND_SOC_BIAS_STANDBY:
477 		/* everything off except vref/vmid, */
478 		snd_soc_update_bits(codec, SSM2602_PWR,
479 			PWR_POWER_OFF | PWR_CLK_OUT_PDN | PWR_OSC_PDN,
480 			PWR_CLK_OUT_PDN | PWR_OSC_PDN);
481 		break;
482 	case SND_SOC_BIAS_OFF:
483 		/* everything off */
484 		snd_soc_update_bits(codec, SSM2602_PWR,
485 			PWR_POWER_OFF, PWR_POWER_OFF);
486 		break;
487 
488 	}
489 	codec->dapm.bias_level = level;
490 	return 0;
491 }
492 
493 #define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
494 		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
495 		SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
496 
497 #define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
498 		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
499 
500 static const struct snd_soc_dai_ops ssm2602_dai_ops = {
501 	.startup	= ssm2602_startup,
502 	.hw_params	= ssm2602_hw_params,
503 	.shutdown	= ssm2602_shutdown,
504 	.digital_mute	= ssm2602_mute,
505 	.set_sysclk	= ssm2602_set_dai_sysclk,
506 	.set_fmt	= ssm2602_set_dai_fmt,
507 };
508 
509 static struct snd_soc_dai_driver ssm2602_dai = {
510 	.name = "ssm2602-hifi",
511 	.playback = {
512 		.stream_name = "Playback",
513 		.channels_min = 2,
514 		.channels_max = 2,
515 		.rates = SSM2602_RATES,
516 		.formats = SSM2602_FORMATS,},
517 	.capture = {
518 		.stream_name = "Capture",
519 		.channels_min = 2,
520 		.channels_max = 2,
521 		.rates = SSM2602_RATES,
522 		.formats = SSM2602_FORMATS,},
523 	.ops = &ssm2602_dai_ops,
524 };
525 
526 static int ssm2602_suspend(struct snd_soc_codec *codec)
527 {
528 	ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
529 	return 0;
530 }
531 
532 static int ssm2602_resume(struct snd_soc_codec *codec)
533 {
534 	snd_soc_cache_sync(codec);
535 
536 	ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
537 
538 	return 0;
539 }
540 
541 static int ssm2602_probe(struct snd_soc_codec *codec)
542 {
543 	struct snd_soc_dapm_context *dapm = &codec->dapm;
544 	int ret;
545 
546 	snd_soc_update_bits(codec, SSM2602_LOUT1V,
547 			    LOUT1V_LRHP_BOTH, LOUT1V_LRHP_BOTH);
548 	snd_soc_update_bits(codec, SSM2602_ROUT1V,
549 			    ROUT1V_RLHP_BOTH, ROUT1V_RLHP_BOTH);
550 
551 	ret = snd_soc_add_codec_controls(codec, ssm2602_snd_controls,
552 			ARRAY_SIZE(ssm2602_snd_controls));
553 	if (ret)
554 		return ret;
555 
556 	ret = snd_soc_dapm_new_controls(dapm, ssm2602_dapm_widgets,
557 			ARRAY_SIZE(ssm2602_dapm_widgets));
558 	if (ret)
559 		return ret;
560 
561 	return snd_soc_dapm_add_routes(dapm, ssm2602_routes,
562 			ARRAY_SIZE(ssm2602_routes));
563 }
564 
565 static int ssm2604_probe(struct snd_soc_codec *codec)
566 {
567 	struct snd_soc_dapm_context *dapm = &codec->dapm;
568 	int ret;
569 
570 	ret = snd_soc_dapm_new_controls(dapm, ssm2604_dapm_widgets,
571 			ARRAY_SIZE(ssm2604_dapm_widgets));
572 	if (ret)
573 		return ret;
574 
575 	return snd_soc_dapm_add_routes(dapm, ssm2604_routes,
576 			ARRAY_SIZE(ssm2604_routes));
577 }
578 
579 static int ssm260x_probe(struct snd_soc_codec *codec)
580 {
581 	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
582 	int ret;
583 
584 	pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
585 
586 	ret = snd_soc_codec_set_cache_io(codec, 7, 9, ssm2602->control_type);
587 	if (ret < 0) {
588 		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
589 		return ret;
590 	}
591 
592 	ret = ssm2602_reset(codec);
593 	if (ret < 0) {
594 		dev_err(codec->dev, "Failed to issue reset: %d\n", ret);
595 		return ret;
596 	}
597 
598 	/* set the update bits */
599 	snd_soc_update_bits(codec, SSM2602_LINVOL,
600 			    LINVOL_LRIN_BOTH, LINVOL_LRIN_BOTH);
601 	snd_soc_update_bits(codec, SSM2602_RINVOL,
602 			    RINVOL_RLIN_BOTH, RINVOL_RLIN_BOTH);
603 	/*select Line in as default input*/
604 	snd_soc_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
605 			APANA_ENABLE_MIC_BOOST);
606 
607 	switch (ssm2602->type) {
608 	case SSM2602:
609 		ret = ssm2602_probe(codec);
610 		break;
611 	case SSM2604:
612 		ret = ssm2604_probe(codec);
613 		break;
614 	}
615 
616 	if (ret)
617 		return ret;
618 
619 	ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
620 
621 	return 0;
622 }
623 
624 /* remove everything here */
625 static int ssm2602_remove(struct snd_soc_codec *codec)
626 {
627 	ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
628 	return 0;
629 }
630 
631 static struct snd_soc_codec_driver soc_codec_dev_ssm2602 = {
632 	.probe =	ssm260x_probe,
633 	.remove =	ssm2602_remove,
634 	.suspend =	ssm2602_suspend,
635 	.resume =	ssm2602_resume,
636 	.set_bias_level = ssm2602_set_bias_level,
637 	.reg_cache_size = ARRAY_SIZE(ssm2602_reg),
638 	.reg_word_size = sizeof(u16),
639 	.reg_cache_default = ssm2602_reg,
640 
641 	.controls = ssm260x_snd_controls,
642 	.num_controls = ARRAY_SIZE(ssm260x_snd_controls),
643 	.dapm_widgets = ssm260x_dapm_widgets,
644 	.num_dapm_widgets = ARRAY_SIZE(ssm260x_dapm_widgets),
645 	.dapm_routes = ssm260x_routes,
646 	.num_dapm_routes = ARRAY_SIZE(ssm260x_routes),
647 };
648 
649 #if defined(CONFIG_SPI_MASTER)
650 static int __devinit ssm2602_spi_probe(struct spi_device *spi)
651 {
652 	struct ssm2602_priv *ssm2602;
653 	int ret;
654 
655 	ssm2602 = devm_kzalloc(&spi->dev, sizeof(struct ssm2602_priv),
656 			       GFP_KERNEL);
657 	if (ssm2602 == NULL)
658 		return -ENOMEM;
659 
660 	spi_set_drvdata(spi, ssm2602);
661 	ssm2602->control_type = SND_SOC_SPI;
662 	ssm2602->type = SSM2602;
663 
664 	ret = snd_soc_register_codec(&spi->dev,
665 			&soc_codec_dev_ssm2602, &ssm2602_dai, 1);
666 	return ret;
667 }
668 
669 static int __devexit ssm2602_spi_remove(struct spi_device *spi)
670 {
671 	snd_soc_unregister_codec(&spi->dev);
672 	return 0;
673 }
674 
675 static struct spi_driver ssm2602_spi_driver = {
676 	.driver = {
677 		.name	= "ssm2602",
678 		.owner	= THIS_MODULE,
679 	},
680 	.probe		= ssm2602_spi_probe,
681 	.remove		= __devexit_p(ssm2602_spi_remove),
682 };
683 #endif
684 
685 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
686 /*
687  * ssm2602 2 wire address is determined by GPIO5
688  * state during powerup.
689  *    low  = 0x1a
690  *    high = 0x1b
691  */
692 static int __devinit ssm2602_i2c_probe(struct i2c_client *i2c,
693 			     const struct i2c_device_id *id)
694 {
695 	struct ssm2602_priv *ssm2602;
696 	int ret;
697 
698 	ssm2602 = devm_kzalloc(&i2c->dev, sizeof(struct ssm2602_priv),
699 			       GFP_KERNEL);
700 	if (ssm2602 == NULL)
701 		return -ENOMEM;
702 
703 	i2c_set_clientdata(i2c, ssm2602);
704 	ssm2602->control_type = SND_SOC_I2C;
705 	ssm2602->type = id->driver_data;
706 
707 	ret = snd_soc_register_codec(&i2c->dev,
708 			&soc_codec_dev_ssm2602, &ssm2602_dai, 1);
709 	return ret;
710 }
711 
712 static int __devexit ssm2602_i2c_remove(struct i2c_client *client)
713 {
714 	snd_soc_unregister_codec(&client->dev);
715 	return 0;
716 }
717 
718 static const struct i2c_device_id ssm2602_i2c_id[] = {
719 	{ "ssm2602", SSM2602 },
720 	{ "ssm2603", SSM2602 },
721 	{ "ssm2604", SSM2604 },
722 	{ }
723 };
724 MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
725 
726 /* corgi i2c codec control layer */
727 static struct i2c_driver ssm2602_i2c_driver = {
728 	.driver = {
729 		.name = "ssm2602",
730 		.owner = THIS_MODULE,
731 	},
732 	.probe = ssm2602_i2c_probe,
733 	.remove = __devexit_p(ssm2602_i2c_remove),
734 	.id_table = ssm2602_i2c_id,
735 };
736 #endif
737 
738 
739 static int __init ssm2602_modinit(void)
740 {
741 	int ret = 0;
742 
743 #if defined(CONFIG_SPI_MASTER)
744 	ret = spi_register_driver(&ssm2602_spi_driver);
745 	if (ret)
746 		return ret;
747 #endif
748 
749 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
750 	ret = i2c_add_driver(&ssm2602_i2c_driver);
751 	if (ret)
752 		return ret;
753 #endif
754 
755 	return ret;
756 }
757 module_init(ssm2602_modinit);
758 
759 static void __exit ssm2602_exit(void)
760 {
761 #if defined(CONFIG_SPI_MASTER)
762 	spi_unregister_driver(&ssm2602_spi_driver);
763 #endif
764 
765 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
766 	i2c_del_driver(&ssm2602_i2c_driver);
767 #endif
768 }
769 module_exit(ssm2602_exit);
770 
771 MODULE_DESCRIPTION("ASoC SSM2602/SSM2603/SSM2604 driver");
772 MODULE_AUTHOR("Cliff Cai");
773 MODULE_LICENSE("GPL");
774