xref: /openbmc/linux/sound/soc/codecs/ak4535.c (revision 8fa5723aa7e053d498336b48448b292fc2e0458b)
1 /*
2  * ak4535.c  --  AK4535 ALSA Soc Audio driver
3  *
4  * Copyright 2005 Openedhand Ltd.
5  *
6  * Author: Richard Purdie <richard@openedhand.com>
7  *
8  * Based on wm8753.c by Liam Girdwood
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 
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 #include <linux/pm.h>
20 #include <linux/i2c.h>
21 #include <linux/platform_device.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/soc-dapm.h>
27 #include <sound/initval.h>
28 
29 #include "ak4535.h"
30 
31 #define AK4535_VERSION "0.3"
32 
33 struct snd_soc_codec_device soc_codec_dev_ak4535;
34 
35 /* codec private data */
36 struct ak4535_priv {
37 	unsigned int sysclk;
38 };
39 
40 /*
41  * ak4535 register cache
42  */
43 static const u16 ak4535_reg[AK4535_CACHEREGNUM] = {
44     0x0000, 0x0080, 0x0000, 0x0003,
45     0x0002, 0x0000, 0x0011, 0x0001,
46     0x0000, 0x0040, 0x0036, 0x0010,
47     0x0000, 0x0000, 0x0057, 0x0000,
48 };
49 
50 /*
51  * read ak4535 register cache
52  */
53 static inline unsigned int ak4535_read_reg_cache(struct snd_soc_codec *codec,
54 	unsigned int reg)
55 {
56 	u16 *cache = codec->reg_cache;
57 	if (reg >= AK4535_CACHEREGNUM)
58 		return -1;
59 	return cache[reg];
60 }
61 
62 static inline unsigned int ak4535_read(struct snd_soc_codec *codec,
63 	unsigned int reg)
64 {
65 	u8 data;
66 	data = reg;
67 
68 	if (codec->hw_write(codec->control_data, &data, 1) != 1)
69 		return -EIO;
70 
71 	if (codec->hw_read(codec->control_data, &data, 1) != 1)
72 		return -EIO;
73 
74 	return data;
75 };
76 
77 /*
78  * write ak4535 register cache
79  */
80 static inline void ak4535_write_reg_cache(struct snd_soc_codec *codec,
81 	u16 reg, unsigned int value)
82 {
83 	u16 *cache = codec->reg_cache;
84 	if (reg >= AK4535_CACHEREGNUM)
85 		return;
86 	cache[reg] = value;
87 }
88 
89 /*
90  * write to the AK4535 register space
91  */
92 static int ak4535_write(struct snd_soc_codec *codec, unsigned int reg,
93 	unsigned int value)
94 {
95 	u8 data[2];
96 
97 	/* data is
98 	 *   D15..D8 AK4535 register offset
99 	 *   D7...D0 register data
100 	 */
101 	data[0] = reg & 0xff;
102 	data[1] = value & 0xff;
103 
104 	ak4535_write_reg_cache(codec, reg, value);
105 	if (codec->hw_write(codec->control_data, data, 2) == 2)
106 		return 0;
107 	else
108 		return -EIO;
109 }
110 
111 static int ak4535_sync(struct snd_soc_codec *codec)
112 {
113 	u16 *cache = codec->reg_cache;
114 	int i, r = 0;
115 
116 	for (i = 0; i < AK4535_CACHEREGNUM; i++)
117 		r |= ak4535_write(codec, i, cache[i]);
118 
119 	return r;
120 };
121 
122 static const char *ak4535_mono_gain[] = {"+6dB", "-17dB"};
123 static const char *ak4535_mono_out[] = {"(L + R)/2", "Hi-Z"};
124 static const char *ak4535_hp_out[] = {"Stereo", "Mono"};
125 static const char *ak4535_deemp[] = {"44.1kHz", "Off", "48kHz", "32kHz"};
126 static const char *ak4535_mic_select[] = {"Internal", "External"};
127 
128 static const struct soc_enum ak4535_enum[] = {
129 	SOC_ENUM_SINGLE(AK4535_SIG1, 7, 2, ak4535_mono_gain),
130 	SOC_ENUM_SINGLE(AK4535_SIG1, 6, 2, ak4535_mono_out),
131 	SOC_ENUM_SINGLE(AK4535_MODE2, 2, 2, ak4535_hp_out),
132 	SOC_ENUM_SINGLE(AK4535_DAC, 0, 4, ak4535_deemp),
133 	SOC_ENUM_SINGLE(AK4535_MIC, 1, 2, ak4535_mic_select),
134 };
135 
136 static const struct snd_kcontrol_new ak4535_snd_controls[] = {
137 	SOC_SINGLE("ALC2 Switch", AK4535_SIG1, 1, 1, 0),
138 	SOC_ENUM("Mono 1 Output", ak4535_enum[1]),
139 	SOC_ENUM("Mono 1 Gain", ak4535_enum[0]),
140 	SOC_ENUM("Headphone Output", ak4535_enum[2]),
141 	SOC_ENUM("Playback Deemphasis", ak4535_enum[3]),
142 	SOC_SINGLE("Bass Volume", AK4535_DAC, 2, 3, 0),
143 	SOC_SINGLE("Mic Boost (+20dB) Switch", AK4535_MIC, 0, 1, 0),
144 	SOC_ENUM("Mic Select", ak4535_enum[4]),
145 	SOC_SINGLE("ALC Operation Time", AK4535_TIMER, 0, 3, 0),
146 	SOC_SINGLE("ALC Recovery Time", AK4535_TIMER, 2, 3, 0),
147 	SOC_SINGLE("ALC ZC Time", AK4535_TIMER, 4, 3, 0),
148 	SOC_SINGLE("ALC 1 Switch", AK4535_ALC1, 5, 1, 0),
149 	SOC_SINGLE("ALC 2 Switch", AK4535_ALC1, 6, 1, 0),
150 	SOC_SINGLE("ALC Volume", AK4535_ALC2, 0, 127, 0),
151 	SOC_SINGLE("Capture Volume", AK4535_PGA, 0, 127, 0),
152 	SOC_SINGLE("Left Playback Volume", AK4535_LATT, 0, 127, 1),
153 	SOC_SINGLE("Right Playback Volume", AK4535_RATT, 0, 127, 1),
154 	SOC_SINGLE("AUX Bypass Volume", AK4535_VOL, 0, 15, 0),
155 	SOC_SINGLE("Mic Sidetone Volume", AK4535_VOL, 4, 7, 0),
156 };
157 
158 /* add non dapm controls */
159 static int ak4535_add_controls(struct snd_soc_codec *codec)
160 {
161 	int err, i;
162 
163 	for (i = 0; i < ARRAY_SIZE(ak4535_snd_controls); i++) {
164 		err = snd_ctl_add(codec->card,
165 			snd_soc_cnew(&ak4535_snd_controls[i], codec, NULL));
166 		if (err < 0)
167 			return err;
168 	}
169 
170 	return 0;
171 }
172 
173 /* Mono 1 Mixer */
174 static const struct snd_kcontrol_new ak4535_mono1_mixer_controls[] = {
175 	SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG1, 4, 1, 0),
176 	SOC_DAPM_SINGLE("Mono Playback Switch", AK4535_SIG1, 5, 1, 0),
177 };
178 
179 /* Stereo Mixer */
180 static const struct snd_kcontrol_new ak4535_stereo_mixer_controls[] = {
181 	SOC_DAPM_SINGLE("Mic Sidetone Switch", AK4535_SIG2, 4, 1, 0),
182 	SOC_DAPM_SINGLE("Playback Switch", AK4535_SIG2, 7, 1, 0),
183 	SOC_DAPM_SINGLE("Aux Bypass Switch", AK4535_SIG2, 5, 1, 0),
184 };
185 
186 /* Input Mixer */
187 static const struct snd_kcontrol_new ak4535_input_mixer_controls[] = {
188 	SOC_DAPM_SINGLE("Mic Capture Switch", AK4535_MIC, 2, 1, 0),
189 	SOC_DAPM_SINGLE("Aux Capture Switch", AK4535_MIC, 5, 1, 0),
190 };
191 
192 /* Input mux */
193 static const struct snd_kcontrol_new ak4535_input_mux_control =
194 	SOC_DAPM_ENUM("Input Select", ak4535_enum[4]);
195 
196 /* HP L switch */
197 static const struct snd_kcontrol_new ak4535_hpl_control =
198 	SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 1, 1, 1);
199 
200 /* HP R switch */
201 static const struct snd_kcontrol_new ak4535_hpr_control =
202 	SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 0, 1, 1);
203 
204 /* mono 2 switch */
205 static const struct snd_kcontrol_new ak4535_mono2_control =
206 	SOC_DAPM_SINGLE("Switch", AK4535_SIG1, 0, 1, 0);
207 
208 /* Line out switch */
209 static const struct snd_kcontrol_new ak4535_line_control =
210 	SOC_DAPM_SINGLE("Switch", AK4535_SIG2, 6, 1, 0);
211 
212 /* ak4535 dapm widgets */
213 static const struct snd_soc_dapm_widget ak4535_dapm_widgets[] = {
214 	SND_SOC_DAPM_MIXER("Stereo Mixer", SND_SOC_NOPM, 0, 0,
215 		&ak4535_stereo_mixer_controls[0],
216 		ARRAY_SIZE(ak4535_stereo_mixer_controls)),
217 	SND_SOC_DAPM_MIXER("Mono1 Mixer", SND_SOC_NOPM, 0, 0,
218 		&ak4535_mono1_mixer_controls[0],
219 		ARRAY_SIZE(ak4535_mono1_mixer_controls)),
220 	SND_SOC_DAPM_MIXER("Input Mixer", SND_SOC_NOPM, 0, 0,
221 		&ak4535_input_mixer_controls[0],
222 		ARRAY_SIZE(ak4535_input_mixer_controls)),
223 	SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0,
224 		&ak4535_input_mux_control),
225 	SND_SOC_DAPM_DAC("DAC", "Playback", AK4535_PM2, 0, 0),
226 	SND_SOC_DAPM_SWITCH("Mono 2 Enable", SND_SOC_NOPM, 0, 0,
227 		&ak4535_mono2_control),
228 	/* speaker powersave bit */
229 	SND_SOC_DAPM_PGA("Speaker Enable", AK4535_MODE2, 0, 0, NULL, 0),
230 	SND_SOC_DAPM_SWITCH("Line Out Enable", SND_SOC_NOPM, 0, 0,
231 		&ak4535_line_control),
232 	SND_SOC_DAPM_SWITCH("Left HP Enable", SND_SOC_NOPM, 0, 0,
233 		&ak4535_hpl_control),
234 	SND_SOC_DAPM_SWITCH("Right HP Enable", SND_SOC_NOPM, 0, 0,
235 		&ak4535_hpr_control),
236 	SND_SOC_DAPM_OUTPUT("LOUT"),
237 	SND_SOC_DAPM_OUTPUT("HPL"),
238 	SND_SOC_DAPM_OUTPUT("ROUT"),
239 	SND_SOC_DAPM_OUTPUT("HPR"),
240 	SND_SOC_DAPM_OUTPUT("SPP"),
241 	SND_SOC_DAPM_OUTPUT("SPN"),
242 	SND_SOC_DAPM_OUTPUT("MOUT1"),
243 	SND_SOC_DAPM_OUTPUT("MOUT2"),
244 	SND_SOC_DAPM_OUTPUT("MICOUT"),
245 	SND_SOC_DAPM_ADC("ADC", "Capture", AK4535_PM1, 0, 0),
246 	SND_SOC_DAPM_PGA("Spk Amp", AK4535_PM2, 3, 0, NULL, 0),
247 	SND_SOC_DAPM_PGA("HP R Amp", AK4535_PM2, 1, 0, NULL, 0),
248 	SND_SOC_DAPM_PGA("HP L Amp", AK4535_PM2, 2, 0, NULL, 0),
249 	SND_SOC_DAPM_PGA("Mic", AK4535_PM1, 1, 0, NULL, 0),
250 	SND_SOC_DAPM_PGA("Line Out", AK4535_PM1, 4, 0, NULL, 0),
251 	SND_SOC_DAPM_PGA("Mono Out", AK4535_PM1, 3, 0, NULL, 0),
252 	SND_SOC_DAPM_PGA("AUX In", AK4535_PM1, 2, 0, NULL, 0),
253 
254 	SND_SOC_DAPM_MICBIAS("Mic Int Bias", AK4535_MIC, 3, 0),
255 	SND_SOC_DAPM_MICBIAS("Mic Ext Bias", AK4535_MIC, 4, 0),
256 	SND_SOC_DAPM_INPUT("MICIN"),
257 	SND_SOC_DAPM_INPUT("MICEXT"),
258 	SND_SOC_DAPM_INPUT("AUX"),
259 	SND_SOC_DAPM_INPUT("MIN"),
260 	SND_SOC_DAPM_INPUT("AIN"),
261 };
262 
263 static const struct snd_soc_dapm_route audio_map[] = {
264 	/*stereo mixer */
265 	{"Stereo Mixer", "Playback Switch", "DAC"},
266 	{"Stereo Mixer", "Mic Sidetone Switch", "Mic"},
267 	{"Stereo Mixer", "Aux Bypass Switch", "AUX In"},
268 
269 	/* mono1 mixer */
270 	{"Mono1 Mixer", "Mic Sidetone Switch", "Mic"},
271 	{"Mono1 Mixer", "Mono Playback Switch", "DAC"},
272 
273 	/* Mic */
274 	{"Mic", NULL, "AIN"},
275 	{"Input Mux", "Internal", "Mic Int Bias"},
276 	{"Input Mux", "External", "Mic Ext Bias"},
277 	{"Mic Int Bias", NULL, "MICIN"},
278 	{"Mic Ext Bias", NULL, "MICEXT"},
279 	{"MICOUT", NULL, "Input Mux"},
280 
281 	/* line out */
282 	{"LOUT", NULL, "Line Out Enable"},
283 	{"ROUT", NULL, "Line Out Enable"},
284 	{"Line Out Enable", "Switch", "Line Out"},
285 	{"Line Out", NULL, "Stereo Mixer"},
286 
287 	/* mono1 out */
288 	{"MOUT1", NULL, "Mono Out"},
289 	{"Mono Out", NULL, "Mono1 Mixer"},
290 
291 	/* left HP */
292 	{"HPL", NULL, "Left HP Enable"},
293 	{"Left HP Enable", "Switch", "HP L Amp"},
294 	{"HP L Amp", NULL, "Stereo Mixer"},
295 
296 	/* right HP */
297 	{"HPR", NULL, "Right HP Enable"},
298 	{"Right HP Enable", "Switch", "HP R Amp"},
299 	{"HP R Amp", NULL, "Stereo Mixer"},
300 
301 	/* speaker */
302 	{"SPP", NULL, "Speaker Enable"},
303 	{"SPN", NULL, "Speaker Enable"},
304 	{"Speaker Enable", "Switch", "Spk Amp"},
305 	{"Spk Amp", NULL, "MIN"},
306 
307 	/* mono 2 */
308 	{"MOUT2", NULL, "Mono 2 Enable"},
309 	{"Mono 2 Enable", "Switch", "Stereo Mixer"},
310 
311 	/* Aux In */
312 	{"Aux In", NULL, "AUX"},
313 
314 	/* ADC */
315 	{"ADC", NULL, "Input Mixer"},
316 	{"Input Mixer", "Mic Capture Switch", "Mic"},
317 	{"Input Mixer", "Aux Capture Switch", "Aux In"},
318 };
319 
320 static int ak4535_add_widgets(struct snd_soc_codec *codec)
321 {
322 	snd_soc_dapm_new_controls(codec, ak4535_dapm_widgets,
323 				  ARRAY_SIZE(ak4535_dapm_widgets));
324 
325 	snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
326 
327 	snd_soc_dapm_new_widgets(codec);
328 	return 0;
329 }
330 
331 static int ak4535_set_dai_sysclk(struct snd_soc_dai *codec_dai,
332 	int clk_id, unsigned int freq, int dir)
333 {
334 	struct snd_soc_codec *codec = codec_dai->codec;
335 	struct ak4535_priv *ak4535 = codec->private_data;
336 
337 	ak4535->sysclk = freq;
338 	return 0;
339 }
340 
341 static int ak4535_hw_params(struct snd_pcm_substream *substream,
342 	struct snd_pcm_hw_params *params)
343 {
344 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
345 	struct snd_soc_device *socdev = rtd->socdev;
346 	struct snd_soc_codec *codec = socdev->codec;
347 	struct ak4535_priv *ak4535 = codec->private_data;
348 	u8 mode2 = ak4535_read_reg_cache(codec, AK4535_MODE2) & ~(0x3 << 5);
349 	int rate = params_rate(params), fs = 256;
350 
351 	if (rate)
352 		fs = ak4535->sysclk / rate;
353 
354 	/* set fs */
355 	switch (fs) {
356 	case 1024:
357 		mode2 |= (0x2 << 5);
358 		break;
359 	case 512:
360 		mode2 |= (0x1 << 5);
361 		break;
362 	case 256:
363 		break;
364 	}
365 
366 	/* set rate */
367 	ak4535_write(codec, AK4535_MODE2, mode2);
368 	return 0;
369 }
370 
371 static int ak4535_set_dai_fmt(struct snd_soc_dai *codec_dai,
372 		unsigned int fmt)
373 {
374 	struct snd_soc_codec *codec = codec_dai->codec;
375 	u8 mode1 = 0;
376 
377 	/* interface format */
378 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
379 	case SND_SOC_DAIFMT_I2S:
380 		mode1 = 0x0002;
381 		break;
382 	case SND_SOC_DAIFMT_LEFT_J:
383 		mode1 = 0x0001;
384 		break;
385 	default:
386 		return -EINVAL;
387 	}
388 
389 	/* use 32 fs for BCLK to save power */
390 	mode1 |= 0x4;
391 
392 	ak4535_write(codec, AK4535_MODE1, mode1);
393 	return 0;
394 }
395 
396 static int ak4535_mute(struct snd_soc_dai *dai, int mute)
397 {
398 	struct snd_soc_codec *codec = dai->codec;
399 	u16 mute_reg = ak4535_read_reg_cache(codec, AK4535_DAC) & 0xffdf;
400 	if (!mute)
401 		ak4535_write(codec, AK4535_DAC, mute_reg);
402 	else
403 		ak4535_write(codec, AK4535_DAC, mute_reg | 0x20);
404 	return 0;
405 }
406 
407 static int ak4535_set_bias_level(struct snd_soc_codec *codec,
408 	enum snd_soc_bias_level level)
409 {
410 	u16 i;
411 
412 	switch (level) {
413 	case SND_SOC_BIAS_ON:
414 		ak4535_mute(codec->dai, 0);
415 		break;
416 	case SND_SOC_BIAS_PREPARE:
417 		ak4535_mute(codec->dai, 1);
418 		break;
419 	case SND_SOC_BIAS_STANDBY:
420 		i = ak4535_read_reg_cache(codec, AK4535_PM1);
421 		ak4535_write(codec, AK4535_PM1, i | 0x80);
422 		i = ak4535_read_reg_cache(codec, AK4535_PM2);
423 		ak4535_write(codec, AK4535_PM2, i & (~0x80));
424 		break;
425 	case SND_SOC_BIAS_OFF:
426 		i = ak4535_read_reg_cache(codec, AK4535_PM1);
427 		ak4535_write(codec, AK4535_PM1, i & (~0x80));
428 		break;
429 	}
430 	codec->bias_level = level;
431 	return 0;
432 }
433 
434 #define AK4535_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\
435 		SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\
436 		SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
437 
438 struct snd_soc_dai ak4535_dai = {
439 	.name = "AK4535",
440 	.playback = {
441 		.stream_name = "Playback",
442 		.channels_min = 1,
443 		.channels_max = 2,
444 		.rates = AK4535_RATES,
445 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
446 	.capture = {
447 		.stream_name = "Capture",
448 		.channels_min = 1,
449 		.channels_max = 2,
450 		.rates = AK4535_RATES,
451 		.formats = SNDRV_PCM_FMTBIT_S16_LE,},
452 	.ops = {
453 		.hw_params = ak4535_hw_params,
454 	},
455 	.dai_ops = {
456 		.set_fmt = ak4535_set_dai_fmt,
457 		.digital_mute = ak4535_mute,
458 		.set_sysclk = ak4535_set_dai_sysclk,
459 	},
460 };
461 EXPORT_SYMBOL_GPL(ak4535_dai);
462 
463 static int ak4535_suspend(struct platform_device *pdev, pm_message_t state)
464 {
465 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
466 	struct snd_soc_codec *codec = socdev->codec;
467 
468 	ak4535_set_bias_level(codec, SND_SOC_BIAS_OFF);
469 	return 0;
470 }
471 
472 static int ak4535_resume(struct platform_device *pdev)
473 {
474 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
475 	struct snd_soc_codec *codec = socdev->codec;
476 	ak4535_sync(codec);
477 	ak4535_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
478 	ak4535_set_bias_level(codec, codec->suspend_bias_level);
479 	return 0;
480 }
481 
482 /*
483  * initialise the AK4535 driver
484  * register the mixer and dsp interfaces with the kernel
485  */
486 static int ak4535_init(struct snd_soc_device *socdev)
487 {
488 	struct snd_soc_codec *codec = socdev->codec;
489 	int ret = 0;
490 
491 	codec->name = "AK4535";
492 	codec->owner = THIS_MODULE;
493 	codec->read = ak4535_read_reg_cache;
494 	codec->write = ak4535_write;
495 	codec->set_bias_level = ak4535_set_bias_level;
496 	codec->dai = &ak4535_dai;
497 	codec->num_dai = 1;
498 	codec->reg_cache_size = ARRAY_SIZE(ak4535_reg);
499 	codec->reg_cache = kmemdup(ak4535_reg, sizeof(ak4535_reg), GFP_KERNEL);
500 
501 	if (codec->reg_cache == NULL)
502 		return -ENOMEM;
503 
504 	/* register pcms */
505 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
506 	if (ret < 0) {
507 		printk(KERN_ERR "ak4535: failed to create pcms\n");
508 		goto pcm_err;
509 	}
510 
511 	/* power on device */
512 	ak4535_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
513 
514 	ak4535_add_controls(codec);
515 	ak4535_add_widgets(codec);
516 	ret = snd_soc_register_card(socdev);
517 	if (ret < 0) {
518 		printk(KERN_ERR "ak4535: failed to register card\n");
519 		goto card_err;
520 	}
521 
522 	return ret;
523 
524 card_err:
525 	snd_soc_free_pcms(socdev);
526 	snd_soc_dapm_free(socdev);
527 pcm_err:
528 	kfree(codec->reg_cache);
529 
530 	return ret;
531 }
532 
533 static struct snd_soc_device *ak4535_socdev;
534 
535 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
536 
537 static int ak4535_i2c_probe(struct i2c_client *i2c,
538 			    const struct i2c_device_id *id)
539 {
540 	struct snd_soc_device *socdev = ak4535_socdev;
541 	struct snd_soc_codec *codec = socdev->codec;
542 	int ret;
543 
544 	i2c_set_clientdata(i2c, codec);
545 	codec->control_data = i2c;
546 
547 	ret = ak4535_init(socdev);
548 	if (ret < 0)
549 		printk(KERN_ERR "failed to initialise AK4535\n");
550 
551 	return ret;
552 }
553 
554 static int ak4535_i2c_remove(struct i2c_client *client)
555 {
556 	struct snd_soc_codec *codec = i2c_get_clientdata(client);
557 	kfree(codec->reg_cache);
558 	return 0;
559 }
560 
561 static const struct i2c_device_id ak4535_i2c_id[] = {
562 	{ "ak4535", 0 },
563 	{ }
564 };
565 MODULE_DEVICE_TABLE(i2c, ak4535_i2c_id);
566 
567 static struct i2c_driver ak4535_i2c_driver = {
568 	.driver = {
569 		.name = "AK4535 I2C Codec",
570 		.owner = THIS_MODULE,
571 	},
572 	.probe =    ak4535_i2c_probe,
573 	.remove =   ak4535_i2c_remove,
574 	.id_table = ak4535_i2c_id,
575 };
576 
577 static int ak4535_add_i2c_device(struct platform_device *pdev,
578 				 const struct ak4535_setup_data *setup)
579 {
580 	struct i2c_board_info info;
581 	struct i2c_adapter *adapter;
582 	struct i2c_client *client;
583 	int ret;
584 
585 	ret = i2c_add_driver(&ak4535_i2c_driver);
586 	if (ret != 0) {
587 		dev_err(&pdev->dev, "can't add i2c driver\n");
588 		return ret;
589 	}
590 
591 	memset(&info, 0, sizeof(struct i2c_board_info));
592 	info.addr = setup->i2c_address;
593 	strlcpy(info.type, "ak4535", I2C_NAME_SIZE);
594 
595 	adapter = i2c_get_adapter(setup->i2c_bus);
596 	if (!adapter) {
597 		dev_err(&pdev->dev, "can't get i2c adapter %d\n",
598 			setup->i2c_bus);
599 		goto err_driver;
600 	}
601 
602 	client = i2c_new_device(adapter, &info);
603 	i2c_put_adapter(adapter);
604 	if (!client) {
605 		dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
606 			(unsigned int)info.addr);
607 		goto err_driver;
608 	}
609 
610 	return 0;
611 
612 err_driver:
613 	i2c_del_driver(&ak4535_i2c_driver);
614 	return -ENODEV;
615 }
616 #endif
617 
618 static int ak4535_probe(struct platform_device *pdev)
619 {
620 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
621 	struct ak4535_setup_data *setup;
622 	struct snd_soc_codec *codec;
623 	struct ak4535_priv *ak4535;
624 	int ret;
625 
626 	printk(KERN_INFO "AK4535 Audio Codec %s", AK4535_VERSION);
627 
628 	setup = socdev->codec_data;
629 	codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
630 	if (codec == NULL)
631 		return -ENOMEM;
632 
633 	ak4535 = kzalloc(sizeof(struct ak4535_priv), GFP_KERNEL);
634 	if (ak4535 == NULL) {
635 		kfree(codec);
636 		return -ENOMEM;
637 	}
638 
639 	codec->private_data = ak4535;
640 	socdev->codec = codec;
641 	mutex_init(&codec->mutex);
642 	INIT_LIST_HEAD(&codec->dapm_widgets);
643 	INIT_LIST_HEAD(&codec->dapm_paths);
644 
645 	ak4535_socdev = socdev;
646 	ret = -ENODEV;
647 
648 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
649 	if (setup->i2c_address) {
650 		codec->hw_write = (hw_write_t)i2c_master_send;
651 		codec->hw_read = (hw_read_t)i2c_master_recv;
652 		ret = ak4535_add_i2c_device(pdev, setup);
653 	}
654 #endif
655 
656 	if (ret != 0) {
657 		kfree(codec->private_data);
658 		kfree(codec);
659 	}
660 	return ret;
661 }
662 
663 /* power down chip */
664 static int ak4535_remove(struct platform_device *pdev)
665 {
666 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
667 	struct snd_soc_codec *codec = socdev->codec;
668 
669 	if (codec->control_data)
670 		ak4535_set_bias_level(codec, SND_SOC_BIAS_OFF);
671 
672 	snd_soc_free_pcms(socdev);
673 	snd_soc_dapm_free(socdev);
674 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
675 	i2c_unregister_device(codec->control_data);
676 	i2c_del_driver(&ak4535_i2c_driver);
677 #endif
678 	kfree(codec->private_data);
679 	kfree(codec);
680 
681 	return 0;
682 }
683 
684 struct snd_soc_codec_device soc_codec_dev_ak4535 = {
685 	.probe = 	ak4535_probe,
686 	.remove = 	ak4535_remove,
687 	.suspend = 	ak4535_suspend,
688 	.resume =	ak4535_resume,
689 };
690 EXPORT_SYMBOL_GPL(soc_codec_dev_ak4535);
691 
692 MODULE_DESCRIPTION("Soc AK4535 driver");
693 MODULE_AUTHOR("Richard Purdie");
694 MODULE_LICENSE("GPL");
695