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