• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/sound/soc/codecs/
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/soc-dapm.h>
42#include <sound/initval.h>
43
44#include "ssm2602.h"
45
46#define SSM2602_VERSION "0.1"
47
48struct snd_soc_codec_device soc_codec_dev_ssm2602;
49
50/* codec private data */
51struct ssm2602_priv {
52	unsigned int sysclk;
53	struct snd_pcm_substream *master_substream;
54	struct snd_pcm_substream *slave_substream;
55};
56
57/*
58 * ssm2602 register cache
59 * We can't read the ssm2602 register space when we are
60 * using 2 wire for device control, so we cache them instead.
61 * There is no point in caching the reset register
62 */
63static const u16 ssm2602_reg[SSM2602_CACHEREGNUM] = {
64	0x0017, 0x0017, 0x0079, 0x0079,
65	0x0000, 0x0000, 0x0000, 0x000a,
66	0x0000, 0x0000
67};
68
69/*
70 * read ssm2602 register cache
71 */
72static inline unsigned int ssm2602_read_reg_cache(struct snd_soc_codec *codec,
73	unsigned int reg)
74{
75	u16 *cache = codec->reg_cache;
76	if (reg == SSM2602_RESET)
77		return 0;
78	if (reg >= SSM2602_CACHEREGNUM)
79		return -1;
80	return cache[reg];
81}
82
83/*
84 * write ssm2602 register cache
85 */
86static inline void ssm2602_write_reg_cache(struct snd_soc_codec *codec,
87	u16 reg, unsigned int value)
88{
89	u16 *cache = codec->reg_cache;
90	if (reg >= SSM2602_CACHEREGNUM)
91		return;
92	cache[reg] = value;
93}
94
95/*
96 * write to the ssm2602 register space
97 */
98static int ssm2602_write(struct snd_soc_codec *codec, unsigned int reg,
99	unsigned int value)
100{
101	u8 data[2];
102
103	/* data is
104	 *   D15..D9 ssm2602 register offset
105	 *   D8...D0 register data
106	 */
107	data[0] = (reg << 1) | ((value >> 8) & 0x0001);
108	data[1] = value & 0x00ff;
109
110	ssm2602_write_reg_cache(codec, reg, value);
111	if (codec->hw_write(codec->control_data, data, 2) == 2)
112		return 0;
113	else
114		return -EIO;
115}
116
117#define ssm2602_reset(c)	ssm2602_write(c, SSM2602_RESET, 0)
118
119/*Appending several "None"s just for OSS mixer use*/
120static const char *ssm2602_input_select[] = {
121	"Line", "Mic", "None", "None", "None",
122	"None", "None", "None",
123};
124
125static const char *ssm2602_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"};
126
127static const struct soc_enum ssm2602_enum[] = {
128	SOC_ENUM_SINGLE(SSM2602_APANA, 2, 2, ssm2602_input_select),
129	SOC_ENUM_SINGLE(SSM2602_APDIGI, 1, 4, ssm2602_deemph),
130};
131
132static const struct snd_kcontrol_new ssm2602_snd_controls[] = {
133
134SOC_DOUBLE_R("Master Playback Volume", SSM2602_LOUT1V, SSM2602_ROUT1V,
135	0, 127, 0),
136SOC_DOUBLE_R("Master Playback ZC Switch", SSM2602_LOUT1V, SSM2602_ROUT1V,
137	7, 1, 0),
138
139SOC_DOUBLE_R("Capture Volume", SSM2602_LINVOL, SSM2602_RINVOL, 0, 31, 0),
140SOC_DOUBLE_R("Capture Switch", SSM2602_LINVOL, SSM2602_RINVOL, 7, 1, 1),
141
142SOC_SINGLE("Mic Boost (+20dB)", SSM2602_APANA, 0, 1, 0),
143SOC_SINGLE("Mic Boost2 (+20dB)", SSM2602_APANA, 7, 1, 0),
144SOC_SINGLE("Mic Switch", SSM2602_APANA, 1, 1, 1),
145
146SOC_SINGLE("Sidetone Playback Volume", SSM2602_APANA, 6, 3, 1),
147
148SOC_SINGLE("ADC High Pass Filter Switch", SSM2602_APDIGI, 0, 1, 1),
149SOC_SINGLE("Store DC Offset Switch", SSM2602_APDIGI, 4, 1, 0),
150
151SOC_ENUM("Capture Source", ssm2602_enum[0]),
152
153SOC_ENUM("Playback De-emphasis", ssm2602_enum[1]),
154};
155
156/* Output Mixer */
157static const struct snd_kcontrol_new ssm2602_output_mixer_controls[] = {
158SOC_DAPM_SINGLE("Line Bypass Switch", SSM2602_APANA, 3, 1, 0),
159SOC_DAPM_SINGLE("Mic Sidetone Switch", SSM2602_APANA, 5, 1, 0),
160SOC_DAPM_SINGLE("HiFi Playback Switch", SSM2602_APANA, 4, 1, 0),
161};
162
163/* Input mux */
164static const struct snd_kcontrol_new ssm2602_input_mux_controls =
165SOC_DAPM_ENUM("Input Select", ssm2602_enum[0]);
166
167static const struct snd_soc_dapm_widget ssm2602_dapm_widgets[] = {
168SND_SOC_DAPM_MIXER("Output Mixer", SSM2602_PWR, 4, 1,
169	&ssm2602_output_mixer_controls[0],
170	ARRAY_SIZE(ssm2602_output_mixer_controls)),
171SND_SOC_DAPM_DAC("DAC", "HiFi Playback", SSM2602_PWR, 3, 1),
172SND_SOC_DAPM_OUTPUT("LOUT"),
173SND_SOC_DAPM_OUTPUT("LHPOUT"),
174SND_SOC_DAPM_OUTPUT("ROUT"),
175SND_SOC_DAPM_OUTPUT("RHPOUT"),
176SND_SOC_DAPM_ADC("ADC", "HiFi Capture", SSM2602_PWR, 2, 1),
177SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &ssm2602_input_mux_controls),
178SND_SOC_DAPM_PGA("Line Input", SSM2602_PWR, 0, 1, NULL, 0),
179SND_SOC_DAPM_MICBIAS("Mic Bias", SSM2602_PWR, 1, 1),
180SND_SOC_DAPM_INPUT("MICIN"),
181SND_SOC_DAPM_INPUT("RLINEIN"),
182SND_SOC_DAPM_INPUT("LLINEIN"),
183};
184
185static const struct snd_soc_dapm_route audio_conn[] = {
186	/* output mixer */
187	{"Output Mixer", "Line Bypass Switch", "Line Input"},
188	{"Output Mixer", "HiFi Playback Switch", "DAC"},
189	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
190
191	/* outputs */
192	{"RHPOUT", NULL, "Output Mixer"},
193	{"ROUT", NULL, "Output Mixer"},
194	{"LHPOUT", NULL, "Output Mixer"},
195	{"LOUT", NULL, "Output Mixer"},
196
197	/* input mux */
198	{"Input Mux", "Line", "Line Input"},
199	{"Input Mux", "Mic", "Mic Bias"},
200	{"ADC", NULL, "Input Mux"},
201
202	/* inputs */
203	{"Line Input", NULL, "LLINEIN"},
204	{"Line Input", NULL, "RLINEIN"},
205	{"Mic Bias", NULL, "MICIN"},
206};
207
208static int ssm2602_add_widgets(struct snd_soc_codec *codec)
209{
210	snd_soc_dapm_new_controls(codec, ssm2602_dapm_widgets,
211				  ARRAY_SIZE(ssm2602_dapm_widgets));
212
213	snd_soc_dapm_add_routes(codec, audio_conn, ARRAY_SIZE(audio_conn));
214
215	return 0;
216}
217
218struct _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 */
228static 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
262static 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
273static 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_device *socdev = rtd->socdev;
280	struct snd_soc_codec *codec = socdev->card->codec;
281	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
282	struct i2c_client *i2c = codec->control_data;
283	u16 iface = ssm2602_read_reg_cache(codec, SSM2602_IFACE) & 0xfff3;
284	int i = get_coeff(ssm2602->sysclk, params_rate(params));
285
286	if (substream == ssm2602->slave_substream) {
287		dev_dbg(&i2c->dev, "Ignoring hw_params for slave substream\n");
288		return 0;
289	}
290
291	/*no match is found*/
292	if (i == ARRAY_SIZE(coeff_div))
293		return -EINVAL;
294
295	srate = (coeff_div[i].sr << 2) |
296		(coeff_div[i].bosr << 1) | coeff_div[i].usb;
297
298	ssm2602_write(codec, SSM2602_ACTIVE, 0);
299	ssm2602_write(codec, SSM2602_SRATE, srate);
300
301	/* bit size */
302	switch (params_format(params)) {
303	case SNDRV_PCM_FORMAT_S16_LE:
304		break;
305	case SNDRV_PCM_FORMAT_S20_3LE:
306		iface |= 0x0004;
307		break;
308	case SNDRV_PCM_FORMAT_S24_LE:
309		iface |= 0x0008;
310		break;
311	case SNDRV_PCM_FORMAT_S32_LE:
312		iface |= 0x000c;
313		break;
314	}
315	ssm2602_write(codec, SSM2602_IFACE, iface);
316	ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
317	return 0;
318}
319
320static int ssm2602_startup(struct snd_pcm_substream *substream,
321			   struct snd_soc_dai *dai)
322{
323	struct snd_soc_pcm_runtime *rtd = substream->private_data;
324	struct snd_soc_device *socdev = rtd->socdev;
325	struct snd_soc_codec *codec = socdev->card->codec;
326	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
327	struct i2c_client *i2c = codec->control_data;
328	struct snd_pcm_runtime *master_runtime;
329
330	/* The DAI has shared clocks so if we already have a playback or
331	 * capture going then constrain this substream to match it.
332	 * TODO: the ssm2602 allows pairs of non-matching PB/REC rates
333	 */
334	if (ssm2602->master_substream) {
335		master_runtime = ssm2602->master_substream->runtime;
336		dev_dbg(&i2c->dev, "Constraining to %d bits at %dHz\n",
337			master_runtime->sample_bits,
338			master_runtime->rate);
339
340		if (master_runtime->rate != 0)
341			snd_pcm_hw_constraint_minmax(substream->runtime,
342						     SNDRV_PCM_HW_PARAM_RATE,
343						     master_runtime->rate,
344						     master_runtime->rate);
345
346		if (master_runtime->sample_bits != 0)
347			snd_pcm_hw_constraint_minmax(substream->runtime,
348						     SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
349						     master_runtime->sample_bits,
350						     master_runtime->sample_bits);
351
352		ssm2602->slave_substream = substream;
353	} else
354		ssm2602->master_substream = substream;
355
356	return 0;
357}
358
359static int ssm2602_pcm_prepare(struct snd_pcm_substream *substream,
360			       struct snd_soc_dai *dai)
361{
362	struct snd_soc_pcm_runtime *rtd = substream->private_data;
363	struct snd_soc_device *socdev = rtd->socdev;
364	struct snd_soc_codec *codec = socdev->card->codec;
365	/* set active */
366	ssm2602_write(codec, SSM2602_ACTIVE, ACTIVE_ACTIVATE_CODEC);
367
368	return 0;
369}
370
371static void ssm2602_shutdown(struct snd_pcm_substream *substream,
372			     struct snd_soc_dai *dai)
373{
374	struct snd_soc_pcm_runtime *rtd = substream->private_data;
375	struct snd_soc_device *socdev = rtd->socdev;
376	struct snd_soc_codec *codec = socdev->card->codec;
377	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
378
379	/* deactivate */
380	if (!codec->active)
381		ssm2602_write(codec, SSM2602_ACTIVE, 0);
382
383	if (ssm2602->master_substream == substream)
384		ssm2602->master_substream = ssm2602->slave_substream;
385
386	ssm2602->slave_substream = NULL;
387}
388
389static int ssm2602_mute(struct snd_soc_dai *dai, int mute)
390{
391	struct snd_soc_codec *codec = dai->codec;
392	u16 mute_reg = ssm2602_read_reg_cache(codec, SSM2602_APDIGI) & ~APDIGI_ENABLE_DAC_MUTE;
393	if (mute)
394		ssm2602_write(codec, SSM2602_APDIGI,
395				mute_reg | APDIGI_ENABLE_DAC_MUTE);
396	else
397		ssm2602_write(codec, SSM2602_APDIGI, mute_reg);
398	return 0;
399}
400
401static int ssm2602_set_dai_sysclk(struct snd_soc_dai *codec_dai,
402		int clk_id, unsigned int freq, int dir)
403{
404	struct snd_soc_codec *codec = codec_dai->codec;
405	struct ssm2602_priv *ssm2602 = snd_soc_codec_get_drvdata(codec);
406	switch (freq) {
407	case 11289600:
408	case 12000000:
409	case 12288000:
410	case 16934400:
411	case 18432000:
412		ssm2602->sysclk = freq;
413		return 0;
414	}
415	return -EINVAL;
416}
417
418static int ssm2602_set_dai_fmt(struct snd_soc_dai *codec_dai,
419		unsigned int fmt)
420{
421	struct snd_soc_codec *codec = codec_dai->codec;
422	u16 iface = 0;
423
424	/* set master/slave audio interface */
425	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
426	case SND_SOC_DAIFMT_CBM_CFM:
427		iface |= 0x0040;
428		break;
429	case SND_SOC_DAIFMT_CBS_CFS:
430		break;
431	default:
432		return -EINVAL;
433	}
434
435	/* interface format */
436	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
437	case SND_SOC_DAIFMT_I2S:
438		iface |= 0x0002;
439		break;
440	case SND_SOC_DAIFMT_RIGHT_J:
441		break;
442	case SND_SOC_DAIFMT_LEFT_J:
443		iface |= 0x0001;
444		break;
445	case SND_SOC_DAIFMT_DSP_A:
446		iface |= 0x0013;
447		break;
448	case SND_SOC_DAIFMT_DSP_B:
449		iface |= 0x0003;
450		break;
451	default:
452		return -EINVAL;
453	}
454
455	/* clock inversion */
456	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
457	case SND_SOC_DAIFMT_NB_NF:
458		break;
459	case SND_SOC_DAIFMT_IB_IF:
460		iface |= 0x0090;
461		break;
462	case SND_SOC_DAIFMT_IB_NF:
463		iface |= 0x0080;
464		break;
465	case SND_SOC_DAIFMT_NB_IF:
466		iface |= 0x0010;
467		break;
468	default:
469		return -EINVAL;
470	}
471
472	/* set iface */
473	ssm2602_write(codec, SSM2602_IFACE, iface);
474	return 0;
475}
476
477static int ssm2602_set_bias_level(struct snd_soc_codec *codec,
478				 enum snd_soc_bias_level level)
479{
480	u16 reg = ssm2602_read_reg_cache(codec, SSM2602_PWR) & 0xff7f;
481
482	switch (level) {
483	case SND_SOC_BIAS_ON:
484		/* vref/mid, osc on, dac unmute */
485		ssm2602_write(codec, SSM2602_PWR, reg);
486		break;
487	case SND_SOC_BIAS_PREPARE:
488		break;
489	case SND_SOC_BIAS_STANDBY:
490		/* everything off except vref/vmid, */
491		ssm2602_write(codec, SSM2602_PWR, reg | PWR_CLK_OUT_PDN);
492		break;
493	case SND_SOC_BIAS_OFF:
494		/* everything off, dac mute, inactive */
495		ssm2602_write(codec, SSM2602_ACTIVE, 0);
496		ssm2602_write(codec, SSM2602_PWR, 0xffff);
497		break;
498
499	}
500	codec->bias_level = level;
501	return 0;
502}
503
504#define SSM2602_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_32000 |\
505		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
506		SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
507
508#define SSM2602_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
509		SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
510
511static struct snd_soc_dai_ops ssm2602_dai_ops = {
512	.startup	= ssm2602_startup,
513	.prepare	= ssm2602_pcm_prepare,
514	.hw_params	= ssm2602_hw_params,
515	.shutdown	= ssm2602_shutdown,
516	.digital_mute	= ssm2602_mute,
517	.set_sysclk	= ssm2602_set_dai_sysclk,
518	.set_fmt	= ssm2602_set_dai_fmt,
519};
520
521struct snd_soc_dai ssm2602_dai = {
522	.name = "SSM2602",
523	.playback = {
524		.stream_name = "Playback",
525		.channels_min = 2,
526		.channels_max = 2,
527		.rates = SSM2602_RATES,
528		.formats = SSM2602_FORMATS,},
529	.capture = {
530		.stream_name = "Capture",
531		.channels_min = 2,
532		.channels_max = 2,
533		.rates = SSM2602_RATES,
534		.formats = SSM2602_FORMATS,},
535	.ops = &ssm2602_dai_ops,
536};
537EXPORT_SYMBOL_GPL(ssm2602_dai);
538
539static int ssm2602_suspend(struct platform_device *pdev, pm_message_t state)
540{
541	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
542	struct snd_soc_codec *codec = socdev->card->codec;
543
544	ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
545	return 0;
546}
547
548static int ssm2602_resume(struct platform_device *pdev)
549{
550	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
551	struct snd_soc_codec *codec = socdev->card->codec;
552	int i;
553	u8 data[2];
554	u16 *cache = codec->reg_cache;
555
556	/* Sync reg_cache with the hardware */
557	for (i = 0; i < ARRAY_SIZE(ssm2602_reg); i++) {
558		data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
559		data[1] = cache[i] & 0x00ff;
560		codec->hw_write(codec->control_data, data, 2);
561	}
562	ssm2602_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
563	return 0;
564}
565
566/*
567 * initialise the ssm2602 driver
568 * register the mixer and dsp interfaces with the kernel
569 */
570static int ssm2602_init(struct snd_soc_device *socdev)
571{
572	struct snd_soc_codec *codec = socdev->card->codec;
573	int reg, ret = 0;
574
575	codec->name = "SSM2602";
576	codec->owner = THIS_MODULE;
577	codec->read = ssm2602_read_reg_cache;
578	codec->write = ssm2602_write;
579	codec->set_bias_level = ssm2602_set_bias_level;
580	codec->dai = &ssm2602_dai;
581	codec->num_dai = 1;
582	codec->reg_cache_size = sizeof(ssm2602_reg);
583	codec->reg_cache = kmemdup(ssm2602_reg, sizeof(ssm2602_reg),
584					GFP_KERNEL);
585	if (codec->reg_cache == NULL)
586		return -ENOMEM;
587
588	ssm2602_reset(codec);
589
590	/* register pcms */
591	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
592	if (ret < 0) {
593		pr_err("ssm2602: failed to create pcms\n");
594		goto pcm_err;
595	}
596	/*power on device*/
597	ssm2602_write(codec, SSM2602_ACTIVE, 0);
598	/* set the update bits */
599	reg = ssm2602_read_reg_cache(codec, SSM2602_LINVOL);
600	ssm2602_write(codec, SSM2602_LINVOL, reg | LINVOL_LRIN_BOTH);
601	reg = ssm2602_read_reg_cache(codec, SSM2602_RINVOL);
602	ssm2602_write(codec, SSM2602_RINVOL, reg | RINVOL_RLIN_BOTH);
603	reg = ssm2602_read_reg_cache(codec, SSM2602_LOUT1V);
604	ssm2602_write(codec, SSM2602_LOUT1V, reg | LOUT1V_LRHP_BOTH);
605	reg = ssm2602_read_reg_cache(codec, SSM2602_ROUT1V);
606	ssm2602_write(codec, SSM2602_ROUT1V, reg | ROUT1V_RLHP_BOTH);
607	/*select Line in as default input*/
608	ssm2602_write(codec, SSM2602_APANA, APANA_SELECT_DAC |
609			APANA_ENABLE_MIC_BOOST);
610	ssm2602_write(codec, SSM2602_PWR, 0);
611
612	snd_soc_add_controls(codec, ssm2602_snd_controls,
613				ARRAY_SIZE(ssm2602_snd_controls));
614	ssm2602_add_widgets(codec);
615
616	return ret;
617
618pcm_err:
619	kfree(codec->reg_cache);
620	return ret;
621}
622
623static struct snd_soc_device *ssm2602_socdev;
624
625#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
626/*
627 * ssm2602 2 wire address is determined by GPIO5
628 * state during powerup.
629 *    low  = 0x1a
630 *    high = 0x1b
631 */
632static int ssm2602_i2c_probe(struct i2c_client *i2c,
633			     const struct i2c_device_id *id)
634{
635	struct snd_soc_device *socdev = ssm2602_socdev;
636	struct snd_soc_codec *codec = socdev->card->codec;
637	int ret;
638
639	i2c_set_clientdata(i2c, codec);
640	codec->control_data = i2c;
641
642	ret = ssm2602_init(socdev);
643	if (ret < 0)
644		pr_err("failed to initialise SSM2602\n");
645
646	return ret;
647}
648
649static int ssm2602_i2c_remove(struct i2c_client *client)
650{
651	struct snd_soc_codec *codec = i2c_get_clientdata(client);
652	kfree(codec->reg_cache);
653	return 0;
654}
655
656static const struct i2c_device_id ssm2602_i2c_id[] = {
657	{ "ssm2602", 0 },
658	{ }
659};
660MODULE_DEVICE_TABLE(i2c, ssm2602_i2c_id);
661/* corgi i2c codec control layer */
662static struct i2c_driver ssm2602_i2c_driver = {
663	.driver = {
664		.name = "SSM2602 I2C Codec",
665		.owner = THIS_MODULE,
666	},
667	.probe = ssm2602_i2c_probe,
668	.remove = ssm2602_i2c_remove,
669	.id_table = ssm2602_i2c_id,
670};
671
672static int ssm2602_add_i2c_device(struct platform_device *pdev,
673				  const struct ssm2602_setup_data *setup)
674{
675	struct i2c_board_info info;
676	struct i2c_adapter *adapter;
677	struct i2c_client *client;
678	int ret;
679
680	ret = i2c_add_driver(&ssm2602_i2c_driver);
681	if (ret != 0) {
682		dev_err(&pdev->dev, "can't add i2c driver\n");
683		return ret;
684	}
685	memset(&info, 0, sizeof(struct i2c_board_info));
686	info.addr = setup->i2c_address;
687	strlcpy(info.type, "ssm2602", I2C_NAME_SIZE);
688	adapter = i2c_get_adapter(setup->i2c_bus);
689	if (!adapter) {
690		dev_err(&pdev->dev, "can't get i2c adapter %d\n",
691		setup->i2c_bus);
692		goto err_driver;
693	}
694	client = i2c_new_device(adapter, &info);
695	i2c_put_adapter(adapter);
696	if (!client) {
697		dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
698		(unsigned int)info.addr);
699		goto err_driver;
700	}
701	return 0;
702err_driver:
703	i2c_del_driver(&ssm2602_i2c_driver);
704	return -ENODEV;
705}
706#endif
707
708static int ssm2602_probe(struct platform_device *pdev)
709{
710	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
711	struct ssm2602_setup_data *setup;
712	struct snd_soc_codec *codec;
713	struct ssm2602_priv *ssm2602;
714	int ret = 0;
715
716	pr_info("ssm2602 Audio Codec %s", SSM2602_VERSION);
717
718	setup = socdev->codec_data;
719	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
720	if (codec == NULL)
721		return -ENOMEM;
722
723	ssm2602 = kzalloc(sizeof(struct ssm2602_priv), GFP_KERNEL);
724	if (ssm2602 == NULL) {
725		kfree(codec);
726		return -ENOMEM;
727	}
728
729	snd_soc_codec_set_drvdata(codec, ssm2602);
730	socdev->card->codec = codec;
731	mutex_init(&codec->mutex);
732	INIT_LIST_HEAD(&codec->dapm_widgets);
733	INIT_LIST_HEAD(&codec->dapm_paths);
734
735	ssm2602_socdev = socdev;
736#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
737	if (setup->i2c_address) {
738		codec->hw_write = (hw_write_t)i2c_master_send;
739		ret = ssm2602_add_i2c_device(pdev, setup);
740	}
741#else
742	/* other interfaces */
743#endif
744	return ret;
745}
746
747/* remove everything here */
748static int ssm2602_remove(struct platform_device *pdev)
749{
750	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
751	struct snd_soc_codec *codec = socdev->card->codec;
752
753	if (codec->control_data)
754		ssm2602_set_bias_level(codec, SND_SOC_BIAS_OFF);
755
756	snd_soc_free_pcms(socdev);
757	snd_soc_dapm_free(socdev);
758#if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
759	i2c_unregister_device(codec->control_data);
760	i2c_del_driver(&ssm2602_i2c_driver);
761#endif
762	kfree(snd_soc_codec_get_drvdata(codec));
763	kfree(codec);
764
765	return 0;
766}
767
768struct snd_soc_codec_device soc_codec_dev_ssm2602 = {
769	.probe = 	ssm2602_probe,
770	.remove = 	ssm2602_remove,
771	.suspend = 	ssm2602_suspend,
772	.resume =	ssm2602_resume,
773};
774EXPORT_SYMBOL_GPL(soc_codec_dev_ssm2602);
775
776static int __init ssm2602_modinit(void)
777{
778	return snd_soc_register_dai(&ssm2602_dai);
779}
780module_init(ssm2602_modinit);
781
782static void __exit ssm2602_exit(void)
783{
784	snd_soc_unregister_dai(&ssm2602_dai);
785}
786module_exit(ssm2602_exit);
787
788MODULE_DESCRIPTION("ASoC ssm2602 driver");
789MODULE_AUTHOR("Cliff Cai");
790MODULE_LICENSE("GPL");
791